ACM-ICPC北京赛区2018 I 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

一定要打一个表看看,然后就会很容易发现规律,无需进行大数判断,最后直接输出答案即可,大数超时。

代码实现:

/*
Look at the star
Look at the shine for U
*/
#include<bits/stdc++.h>
#define ll long long
#define PII pair<int,int>
#define sl(x) scanf("%lld",&x)
using namespace std;
const int N = 1e6+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
char s[N];
int main()
{
    ll t,i,j,k;
    sl(t);
    while(t--)
    {
         scanf("%s",s);
         ll len = strlen(s);
         if(len <= 2 && s[1] <= '0' && s[0] <= '1')
         {
                ll sum = 0;
                for(i = 0;s[i];i++)
                    sum = sum*10+(s[i]-'0');
                printf("%lld\n",sum-1);
                continue;
         }
         if(s[0] == '1' && s[1] != '0')
         {
            for(i = 1;i < len;i++) printf("%c",s[i]);
            for(i = len - 1;i >= 1;i--) printf("%c",s[i]);
            puts("");
         }
         else 
         {
            if(s[0] >= '2')
            {
                for(i = 0;i < len;i++) printf("%c",s[i]-(!i));
                for(i = len - 2;i >= 0;i--) printf("%c",s[i]-(!i));
                puts("");
            }
            else
            {
                char temp = (s[0]-'0')*10+(s[1]-'0')-1+'0';
                printf("%c",temp);
                for(i = 2;i < len;i++) printf("%c",s[i]);
                for(i = len - 2;i >= 2;i--) printf("%c",s[i]);
                printf("%c\n",temp);
            }
         }
    } 
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值