码迷,mamicode.com
首页 > 其他好文 > 详细

zoj2000 Palindrome Numbers

时间:2015-04-16 09:10:30      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:zoj

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 5 6 7 8 9

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

1 2 3 4 5 6 7 8 9

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

10 11 12 13 14 15 16 17 18 19

20 21 22 23 24 25 26 27 28 29

……

90 91 92 93 94 95 96 97 98 99

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

10 11 12 13 14 15 16 17 18 19

20 21 22 23 24 25 26 27 28 29

……

90 91 92 93 94 95 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++;
			}
			else
				break;
			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;
}


参考代码:


zoj2000 Palindrome Numbers

标签:zoj

原文地址:http://blog.csdn.net/codeforcer/article/details/45066729

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!