Problem Statement
|
|
You enjoy working with numbers that contain only square digits (namely, 0, 1, 4 and 9). The sequence containing only these digits is 0, 1, 4, 9, 10, 11, 14... Return the
n-th term (indexed from 0) in this sequence. |
Definition
|
|
Class: |
SquareDigitNumbers |
Method: |
getNumber |
Parameters: |
int |
Returns: |
int |
Method signature: |
int getNumber(int n) |
(be sure your method is public) |
|
Limits
|
|
Time limit (s): |
2.000 |
Memory limit (MB): |
64 |
|
Constraints
|
- |
n will be between 0 and 1000, inclusive. |
Examples
|
0) |
|
|
|
Returns: 0
|
The first square digit number is 0. |
|
|
1) |
|
|
|
2) |
|
|
|
3) |
|
|
|
4) |
|
|
|
解题思路:
第一次在TC上切题,还是经过菠萝爷的指导才慢慢开始做的,总的来说,div2的250分的题都是简单题,只要认真推敲下,就能切出来..
这题的意思是说,有一些数,这些数字都是由0 1 4 9组成的,那么输入一个n,让你输出这个数,注意下标是从0开始的.然后呢,将只含有0 1 4 9的数字从
i = 0 ->i = 100000变化,只要求出了满足这个条件的第一个数字,就把他按顺序放进a数组中,注意,也是要从0开始放,一直到最后一个数字判断结束,然后直接根据输入的n的值返回我们需要的结果就OK了.
代码:
# include<cstdio>
# include<iostream>
using namespace std;
# define MAX 100000
int a[MAX];
class SquareDigitNumbers
{
public:
int getNumber( int n )
{
int flag;
int i;
int j = 0;
for ( i = 0;i <= 100000;i++ )
{
int x = i;
flag = 1;
while ( x != 0 )
{
if ( x%10 == 0 ||x%10 == 1||x%10 == 4||x%10 == 9 )
{
x /= 10;
continue;
}
flag = 0;
break;
}
if ( flag )
{
a[j++] = i;
}
}
return a[n];
}
};