poj 2402 Palindrome Numbers

Palindrome Numbers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3948 Accepted: 1505

Description

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

Source



这道题得找区间,1位数和2位数的回文数都是9个,3位数和4位数的回文数都是9*10个,5位和6位都是9*10*10个,那么就可以找到所求数字的位数了,然后就是从小到大二分查找所求的数了,并且只要找前一半就好了。



#include <iostream>
#include <cstdio>
using namespace std;
long long int find(long long int  n){
	long long int  ans,i,j,pos,cnt,tp,left,right;
	cnt=0;
	tp=9;
	for(i=1;i;i++){
		if(cnt+tp>=n){
			pos=i;
			break;
		}
		cnt+=tp;
		if(i%2!=1) tp*=10;
	}
	n-=cnt;
	left=1;
	for(i=2;i<=(pos+1)/2;i++){
		left*=10;
	}
	ans=left+=n-1;
	if(pos%2==1) 
		left/=10;
	while(left){
		ans=ans*10+left%10;
		left/=10;
	}
	return ans;
}

int main(){
	long long int  n;
	while(cin>>n,n){
		printf("%I64d\n",find(n));
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值