Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=3202
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 be ordered in size. The first few palindrome numbers are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, ...
The number 10 is not a palindrome (even though you could write it as 010) but a zero as leading digit is not allowed.
1 12 24 0
1 33 151
打表,先根据表找出这个回文数有多少位,然后算一般,另一半照前面的输出。
完整代码:
/*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] & 15) { len = strlen(str); num = atoi(str); if (len == 1) putchar(str[0]); else if (num < 19) { num += 39; putchar(num);putchar(num); } else { i = 2; while (endsum[++i] < num) ; /// 此时回文数的位数为i num -= endsum[i - 1] + 1; /// 多减一个 /// 现在num是第num个i位数的回文数 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)///优先级啊。。 putchar(str[maxj - 1]); for (j = maxj - 2; j >= 0; --j) putchar(str[j]); } putchar('\n'); } return 0; }