12050-palindrome Numbers
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php? option=com_onlinejudge&itemid=8&category=467&page=show_problem&problem=32 02
A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, the name ' Anna ' is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionally numbers can of course is ordered in size. The palindrome numbers are: 1, 2, 3, 4, 5, 6, 7, 8, 9 , one, few, ...
The number is not a palindrome (even though your could write it as 010) but a zero as leading digit is Not allowed.
Input
The input consists of a series of lines with each line containing one integer value I (1 <= i <= 2*109). This integer value I indicates the index of the palindrome number, which is to being written to the output, where Index 1 stand S for the "palindrome" number (1), index 2 stands for the second palindrome number (2) and. The input is terminated by a line containing 0.
Output
For each line of input (except "last one") exactly one line of output containing a single (decimal) integer value was to Be produced. For each input value i the i-th palindrome number are to being written to the output.
Sample Input
1
0
Sample Output
1
151
Hit the table, first based on the table to find out how many digits of the palindrome, and then the general, the other half according to the previous output.
Complete code:
/*0.015s*/#include <cstdio> #include <cstring> #include <cstdlib> const int endsum[21] = {0, 9, 18, 108, 198, 1098, 1998, 10998, 19998, 109998, 199998, 1099998, 1999998, 10999998, 19999998, 109999998, 199999998
, 1099999998, 1999999998, 2000000001};
Char str[20];
int main (void) {int len, num, I, J, Maxj;
int ans;
while (gets (str), str[0] &) {len = strlen (str);
num = atoi (str);
if (len = = 1) putchar (str[0]);
else if (num <) {num = 39;
Putchar (num);p Utchar (num);
else {i = 2;
while (Endsum[++i] < num); At this time the number of palindrome number is I num-= endsum[i-1] + 1;
Subtract one more///now num is the number of num i digits of palindrome ans = 1; MAXJ = (i + 1) >> 1;
I/2 for (j = 1; j < Maxj; ++j) ans *= 10;
ans = num; sprintf (str, "%d", ans);
for (j = 0; j < Maxj; ++j) Putchar (str[j));
if ((i & 1) = = 0)///priority ah ...
Putchar (Str[maxj-1]);
for (j = maxj-2 J >= 0;--j) Putchar (str[j));
} putchar (' \ n ');
return 0; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/