Palindromes HihoCoder - 1878(找规律)

Recently, Nvoenewr learnt palindromes in his class.

A palindrome is a nonnegative integer that is the same when read from left to right and when read from right to left. For example, 0, 1, 2, 11, 99, 232, 666, 998244353353442899 are palindromes, while 10, 23, 233, 1314 are not palindromes.

Now, given a number, Nvoenewr can determine whether it's a palindrome or not by using loops which his teacher has told him on the class. But he is now interested in another question: What's the K-th palindrome? It seems that this question is too difficult for him, so now he asks you for help.

Nvoenewr counts the number from small to big, like this: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101 and so on. So the first palindrome is 0 and the eleventh palindrome is 11 itself.
Nvoenewr may ask you several questions, and the K may be very big.

Input

The first line contains one integer T(T <= 20) —— the number of questions that Nvoenewr will ask you.

Each of the next T lines contains one integer K. You should find the K-th palindrome for Nvoenewr.

Let's say K is a n-digit number. It's guaranteed that K >= 1, 1 <= n <= 100000 and the sum of n in all T questions is not greater than 1000000.

Output

Print T lines. The i-th line contains your answer of Nvoenewr's i-th question.

Sample Input

4
1
10
11
20

Sample Output

0
9
11
101

 题目意思:

让你找到第k个回文数,并输出,k很大很大。

思路:

打表找规律,其实没什么方法,如果没找到规律,只能说观察的不够仔细。比赛的时候我是找到规律了,不过太过杂乱,没有整合起来。。。顺便说一说从中学到的一点东西,分情况的时候,先把最普遍的情况拉出来,然后开始考虑上下边界情况,这样一般就能想的比较全,比较有条理了。

 

注释掉的是打表的代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

vector < char > ans;
// bool judge(int k)
// {
// 	int a[105], cnt = 0;
// 	while(k)
// 	{
// 		a[cnt++] = k % 10;
// 		k = k / 10;
// 	}
// 	int i = 0, j = cnt - 1;
// 	while(i < j)
// 	{
// 		if(a[i++] != a[j--])
// 			return false;
// 	}
// 	return true;
// }

void Print()
{
	for(int i = 0; i < (int)ans.size(); ++ i)
	{
		cout << ans[i];
	}
	cout << endl;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	int t;
	cin >> t;
	while(t --)
	{
		string str;
		ans.clear();
		cin >> str;
		int len = str.size();
		if(len == 1)
		{
			cout << (int)(str[0] - '0' - 1) << endl;
			continue;
		}
		if(str == "10")
		{
			cout << 9 << endl;
			continue;
		}
		if(str[0] == '1')
		{
			if(str[1] == '0')
			{
				ans.push_back('9');
				for(int i = 2; i < len; ++ i)
				{
					ans.push_back(str[i]);
				}
				for(int i = len - 2; i >= 2; -- i)
				{
					ans.push_back(str[i]);
				}
				ans.push_back('9');
				Print();
			}
			else
			{
				for(int i = 1; i < len; ++ i)
				{
					ans.push_back(str[i]);
				}
				for(int i = len - 1; i >= 1; -- i)
				{
					ans.push_back(str[i]);
				}
				Print();
			}
		}
		else
		{
			ans.push_back(str[0] - 1);
			for(int i = 1; i < len; ++ i)
			{
				ans.push_back(str[i]);
			}
			for(int i = len - 2; i >= 1; -- i)
			{
				ans.push_back(str[i]);
			}
			ans.push_back(str[0] - 1);
			Print();

		}
	}
	// int num = 0;
	// for(int i = 0 ; i <= 1000000; ++ i)
	// {
	// 	if(judge(i))
	// 		cout << ++num << ' ' << i << endl;
	// }
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值