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∗109). 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
0
Sample Output
1
33
151
思路 1,2,3,4,5,6,7,8,9
11,22,33,44,55,66,77,88,99
xox 9*10;中间可以填零
xoox 9*10;
xooox 9*10*10
xoooox 9*10*10
xooooox 9*10*10*10
xoooooox 9*10*10*10
规律已经出来了。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <set> #include <map> #include <algorithm> using namespace std; typedef long long ll; ll a[10],b[20],cnt[20]; void init() { a[0]=1; for(int i=1;i<10;i++){ a[i]=a[i-1]*10; } for(int i=0;i<20;i+=2){ b[i]=b[i+1]=a[i/2]*9; } cnt[0]=9; for(int i=1;i<20;i++){ cnt[i]=cnt[i-1]+b[i]; } } int main() { ll n,pos,ans,inf; init(); while(cin>>n && n) { pos=0; int s[35]; pos=lower_bound(cnt,cnt+20,n)-cnt; ans=a[pos/2]+(pos>0?n-1-cnt[pos-1]:n-1); inf=0; while(ans) { s[inf++]=ans%10; ans/=10; } for(int i=inf-1;i>=0;i--) printf("%d",s[i]); for(int i=pos%2?0:1;i<inf;i++) printf("%d",s[i]); printf(" "); } return 0; }
再来一个反向求?
"回文数"是一种数 字。如:98789, 这个数字正读是98789,倒读也是98789,正读倒读一样,所以这个数字就是回文数。现在给你一个回文数n(0<n<1019),让你求出这个数是第几个的回文正整数。
#include <iostream> #include <cstdio> using namespace std; typedef long long ll; ll a[10],b[20],num[20]; void ac() { a[0]=1; for(int i=1;i<10;i++){ a[i]=a[i-1]*10; } for(int i=0;i<20;i+=2){ b[i]=b[i+1]=a[i/2]*9; } num[0]=9; for(int i=1;i<20;i++){ num[i]=num[i-1]+b[i]; } } int find(int n) { ac(); int pos[20]; int cnt=0,k; while(n) { pos[cnt++]=n%10; n/=10; } if(cnt%2==1) k=cnt/2; else k=cnt/2-1; int ans=0; int m=cnt-1; for(int i=0;i<=k;i++) { ans=ans*10+pos[i]; } if(m==0)return ans-a[m/2]+1; else return ans-a[m/2]+num[m-1]+1; } int main() { int n; while(cin>>n) { cout<<find(n)<<endl; } return 0; }