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 as010) but a zero as leading digit is not allowed.
1 12 24 0
1 33 151
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; typedef long long LL; LL ten[20]; LL num[20]; LL sum[20]; void init() { ten[0]=1; for(int i=1;i<10;i++) ten[i]=ten[i-1]*10; for(int i=0;i<=20;i+=2) num[i] = num[i+1] = ten[i/2]*9; sum[0]=num[0]; for(int i=1;i<=20;i++) sum[i]=sum[i-1]+num[i]; } int str[1000]; int top; void LLtoStr(LL n) { top=0; while(n) str[top++]=n%10,n/=10; } void work(LL n) { int pos = lower_bound(sum,sum+20,n)-sum; LL ans = ten[pos/2]+(pos>0?n-sum[pos-1]-1:n-1); LLtoStr(ans); for(int i=top-1;i>=0;i--) printf("%d",str[i]); for(int i=pos&1?0:1;i<top;i++) printf("%d",str[i]); puts(""); } int main() { init(); LL n; while(cin>>n && n) work(n); }