Description
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.Input
The input consists of a series of lines with each line containing one integer value i (1<= i <= 2*10^9 ). This integer value i indicates the index of the palindrome number that is to be written to the output, where index 1 stands for the first palindrome number (1), index 2 stands for the second palindrome number (2) and so on. The input is terminated by a line containing 0.Output
For each line of input (except the last one) exactly one line of output containing a single (decimal) integer value is to be produced. For each input value i the i-th palindrome number is to be written to the output.Sample Input
1 12 24 0Sample Output
1 33 151Source
Tehran 2003 Preliminary题意:
输出第n个回文数字。
POINT:
一直用搜索做,做不出。
先求出第n个的长度,然后截一半,从10**0开始数,第k个就是答案。
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> using namespace std; #define LL long long LL a[20]; void init() { a[1]=9; a[2]=9; for(int i=3;;i++) { a[i]=a[i-2]*10; if(a[i]>=2*1e9) break; } } int main() { LL n; init(); while(~scanf("%lld",&n)&&n) { LL now=0; int i; for(i=1;;i++) { now+=a[i]; if(now>=n) break; } int k=i; n=n-now+a[i]; int l; if(k&1) l=k/2+1; else l=k/2; now=(LL)pow(10,l-1); LL ans=now+n-1; printf("%lld",ans); int anss[20]; for(int i=1;i<=l;i++) { anss[i]=ans; ans/=10; } if(k&1) i=2; else i=1; for(;i<=l;i++) { printf("%d",anss[i]); } printf("\n"); } }