POJ-2402(Palindrome Numbers)

【题目描述】

求第n个回文数

【解题思路】

首先求出一共k位长的数一共多少个回文,然后对半开

/* list[i]表示位数为i的满足回文数要求共有list[i]个 */
ll List[20] = {0, 9, 9, 90, 90, 900, 900, 9000, 9000, 90000, 90000, 900000, 900000, 9000000, 9000000, 90000000,
               90000000, 900000000, 900000000, 9000000000};
int main()
{
	ll n, sum;
	int i;
	while (cin>>n && n) {
		sum = 0;
		for (i = 1; i <= 19; ++i) {/* i表示位数 */
			if (sum + List[i] >= n)//i表示位数
				break;
			sum += List[i];
		}
		string str(i, '0');
		str[0] = str[i - 1] = '1';
		n = n - sum - 1;
		if (i == 1) {
			str[0] += n;
		} else {
			int index = -1, s = (int)pow(10.0, (i - 2) / 2 + (i - 2) % 2);
			for ( ; s >= 1 && n != 0 ;s /= 10) {
				index++;
				if (n < s)
					continue;
				str[index] += n / s;/* 不能写成=n/s,因为最外层的str[0],str[i-1]起始值是1 */
				str[i - 1 - index] = str[index];
				n %= s;
			}
		}
		cout<<str<<endl;
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值