1. 程式人生 > >【hiho1878】Palindromes(規律)

【hiho1878】Palindromes(規律)

題目連結

#1878 : Palindromes

時間限制: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<bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        string str;
        cin>>str;
        if(str.size()==1)
        {
            printf("%d\n",str[0]-'0'-1);
            continue;
        }
        if(str=="10")
        {
            printf("9\n");
            continue;
        }
        vector<int>v;
        if(str[0]=='1')
        {
            if(str[1]!='0')
            {
                for(int i=1;i<str.size();i++)
                    v.push_back(str[i]-'0');
                for(int i=str.size()-1;i>=1;i--)
                    v.push_back(str[i]-'0');
            }
            else
            {
                v.push_back(9);
                for(int i=2;i<str.size();i++)
                    v.push_back(str[i]-'0');
                for(int i=str.size()-2;i>=2;i--)
                    v.push_back(str[i]-'0');
                v.push_back(9);
            }
        }
        else
        {
            v.push_back(str[0]-'0'-1);
            for(int i=1;i<str.size();i++)
                v.push_back(str[i]-'0');
            for(int i=str.size()-2;i>=1;i--)
                v.push_back(str[i]-'0');
            v.push_back(str[0]-'0'-1);
        }
        for(int i=0;i<v.size();i++)
            printf("%d",v[i]);
        printf("\n");
    }
}
/*
#include<bits/stdc++.h>
using namespace std;
int is(int x)
{
    vector<int>v;
    while(x)
    {
        int t=x%10;
        v.push_back(t);
        x/=10;
    }
    for(int i=0;i<v.size()/2;i++)
    {
        if(v[i]!=v[v.size()-1-i])return 0;
    }
    return 1;
}
int main()
{
    int k=0;
    for(int i=0;i<=100000;i++)
    {
        if(is(i))
        {
            printf("k=%d , num=%d\n",++k,i);
        }
    }
}

*/