zoj2000 Palindrome Numbers

来源:互联网 发布:程序员需要读的书 编辑:程序博客网 时间:2024/05/18 03:29

Palindrome Numbers

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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
0


Sample Output

1
33
151



题意:求第i个回文数字;

解析:

位数为1的回文个数:9

位数为2的回文个数:9

位数为3的回文个数:90

位数为4的回文个数:90

位数为5的回文个数:900

位数为6的回文个数:900

位数为7的回文个数:9000

位数为8的回文个数:90000

.……

位数为n的回文个数:9*(10^((n-1)/2)),

将这些回文数拆分一半,可以发现

---------------------------------------------------------1位

1 2 3 4 56 7 8 9

---------------------------------------------------------2位

1 2 3 4 56 7 8 9

---------------------------------------------------------3位

10 11 12 13 1415 16 17 18 19

20 21 22 23 2425 26 27 28 29

……

90 91 92 93 9495 96 97 98 99

----------------------------------------------------------4位

10 11 12 13 1415 16 17 18 19

20 21 22 23 2425 26 27 28 29

……

90 91 92 93 9495 96 97 98 99

---------------------------------------------------------5位

100 101 102 103 104 105 106 107 108 109

110 111 112 113 114 115 116 117 118 119

……

990 991 992 993 994 995 996 997 998 999

---------------------------------------------------------6位

……

参考代码:

#include <iostream>using namespace std;typedef long long ll;ll pow(int a,int b){ll ans = 1;for (int i=0;i<b;i++){ans*=a;}return ans;}int main(){ll n;while (cin>>n&&n){int count=1;int q=9;while (n>q){n-=q;count++;if (n>q){n-=q;count++;}elsebreak;q*=10;}//cout<<"n="<<n<<endl;int k=(count-1)/2;//cout<<"k="<<k<<endl;ll ans=pow(10,k)+n-1;//cout<<"ans="<<ans<<endl;//cout<<"count="<<count<<endl;int a[20],s=0;cout<<ans;while (ans){a[s++]=ans%10;ans/=10;}for (int j=count/2-1;j>=0;j--){if (j<0)break;cout<<a[s-1-j];}cout<<endl;}return 0;}


参考代码:


0 0