2018ACM-ICPC北京赛区 - I:Palindromes(打表找规律)
发布日期:2021-07-01 00:14:56 浏览次数:3 分类:技术文章

本文共 2232 字,大约阅读时间需要 7 分钟。

时间限制:1000ms 单点时限:1000ms 内存限制:512MB

描述

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.

输入

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.

输出

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

样例输入

4

1
10
11
20

样例输出

0

9
11
101

解题思路

这一题就是一道找规律的题,k和答案是有关系的。

#include 
#include
char str[100010];int main(){ int t, len; scanf("%d", &t); while (t--) { scanf("%s", str); len = strlen(str); if (len == 1) printf("%c\n", str[0] - 1); else if (len == 2 && str[0] == '1' && str[1] == '0') printf("9\n"); else if (str[0] == '1' && str[1] == '0') { printf("9"); for (int i = 2; i < len; i++) printf("%c", str[i]); for (int i = len - 2; i > 1; i--) printf("%c", str[i]); printf("9\n"); } else if (str[0] == '1' && str[1] > '0') { for (int i = 1; i < len; i++) printf("%c", str[i]); for (int i = len - 1; i > 0; i--) printf("%c", str[i]); printf("\n"); } else { printf("%c", str[0] - 1); for (int i = 1; i < len; i++) printf("%c", str[i]); for (int i = len - 2; i > 0; i--) printf("%c", str[i]); printf("%c\n", str[0] - 1); } } return 0;}

 

转载地址:https://lzyws739307453.blog.csdn.net/article/details/84192595 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:2014 ACM-ICPC - A:Built with Qinghuai and Ari Factor
下一篇:2018ACM-ICPC北京赛区 - A:Jin Yong’s Wukong Ranking List(DFS)

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月25日 05时51分18秒