2018 ACM-ICPC北京赛区 Palindromes 【回文数】

版权声明:本文为博主原创文章,转载请注明出处( • ̀ω•́ )✧ https://blog.csdn.net/wangws_sb/article/details/84192064

题目传送门

时间限制: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个回文数字,K的长度范围是1e5。

解题思路:我们很容易看出来,长度为1的回文数的个数是10,长度为2的回文数的个数是9,长度为3的回文串的个数是90,长度为4的回文串的个数是90,以此类推,后面依次是900,900,9000,9000,90000,90000……找到这个规律以后我们就能算出第k个回文数字是长度为n的回文数的第m个,回文数是对称的,所以我们只需要输出前一半,然后后一半逆序输出就好了,而长度为n的第m个的前一半是m+99…((n-1)/2个9 ),输出回文数的前一半,若n为偶数,逆序输出前一半,若n为奇数,除去最后一位以外逆序输出前一半,n为1的时候特殊处理直接输出m-1就行。

可是我们发现K的长度范围是1e5,注意是长度范围而不是数据范围,所以按上面的方法计算是非常不现实的。其实这道题的正确打开方式是找规律,按字符串来处理,分为以下几种情况:(可以输出前1e9个打表找规律)

1. 字符串长度为1的时候直接输出s[0]-1;

2. 字符串长度为2且s[0]=1,s[1]=0的时候直接输出9;

3. s[0]=1且s[1]=0的时候

4. s[0]=1且s[1]!=0的时候

5. s[0]!=1的时候

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int T;
char s[100100];
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        int len=strlen(s);
        if(len==1)//字符串长度为1的时候直接输出s[0]-1
        {
            printf("%c\n",s[0]-1);
            continue;
        }
        if(len==2&&s[0]=='1'&&s[1]=='0')//字符串长度为2且s[0]=1,s[1]=0的时候直接输出9
        {
            printf("9\n");
            continue;
        }
        if(s[0]=='1')
        {
            if(s[1]=='0')//s[0]=1且s[1]=0的时候
            {
                printf("9");
                for(int i=2;i<len;i++)
                    printf("%c",s[i]);
                for(int i=len-2;i>1;i--)
                    printf("%c",s[i]);
                printf("9\n");
            }
            else//s[0]=1且s[1]!=0的时候
            {
                for(int i=1;i<len;i++)
                    printf("%c",s[i]);
                for(int i=len-1;i>0;i--)
                    printf("%c",s[i]);
                printf("\n");
            }
        }
        else//s[0]!=1的时候
        {
            printf("%c",s[0]-1);
            for(int i=1;i<len;i++)
                printf("%c",s[i]);
            for(int i=len-2;i>0;i--)
                printf("%c",s[i]);
            printf("%c\n",s[0]-1);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wangws_sb/article/details/84192064