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). Additionallynumbers 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 leadingdigit is not allowed.
题意:让你求第N个回文数字。
思路:可以算出来第N个回文数字,长度多长而且是该长度第几个回文串。因为1位数有9个,2位数9个,3位数90,4位数90,可以预处理出来。
当我们知道长度的时候,也知道是第几个的时候,直接将第几个-1然后加上一半长度的回文数字。
比如:24 可以求出他的回文长度是3,是第6个。
回文长度为3,他的第一个数101,他的左半部分是10
6-1=5; 用5直接加上他的左半部分就可以了。15
那么将15反着输出,并且他的长度为3.所以只输出1.
则就是151;
#include<stdio.h> #include<string.h> const int N=1<<29; const long long maxn=2e11; long long num[10000]; int cnt=0; int a[100],b[100],kb; void init() { long long t=90; num[1]=9,num[2]=18; cnt=2; for(int i=3;; i+=2) { num[i]+=num[i-1]+t; num[i+1]+=num[i]+t; t*=10; cnt+=2; if(t>maxn)break; } } long long get(int x) { long long t=9; x--; while(x--) { t*=10; } return t; } int chuli(int len,long long cc) { cc--; int f=0; if(len%2==0)f=1; len=(len+1)/2; memset(b,0,sizeof(b)); kb=1; //printf("%lld\n",cc); while(cc>0) { int kk=cc%10; cc/=10; b[kb++]=kk; } b[len]+=1; if(f==0) { for(int i=len;i>1;i--) printf("%d",b[i]); } else { for(int i=len;i>=1;i--) printf("%d",b[i]); } for(int i=1; i<=len; i++) printf("%d",b[i]); puts(""); } int main() { init(); int n; while(scanf("%d",&n)!=EOF) { if(!n)break; if(n<=9) { printf("%d\n",n); continue; } int t=1; for(int i=1; i<=cnt; i++) if(n<=num[i]) { t=i; break; } long long cc=n-num[t-1]; //printf("%d %d\n",t,cc); chuli(t,cc); } return 0; }