zoj2000 Palindrome Numbers

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;
}


参考代码:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值