Palindrome Magic 输出n位数的第k个回文串 codechef赛事

http://www.codechef.com/BTCD2012/problems/T02/

Palindrome Magic

Problem code: T02

In Math-World, each student is issued a unique ID which is a palindromic number. This ensures that the students can easily remember their ID. But the administration is afraid of a problem. It is afraid that if it keeps a very small ID, then it may soon run out of unique palindrome numbers. Thus, it has hired you as they've run out of mathematicians and programmers. So, they tell you the problem and then ask you to solve the following problem. For a n-digit palindrome, what is the kth palindrome (if arranged in ascending order) that can be formed?For eg. In the list of 2 digit palindromes, the 7th palindrome is 77 (1st being 11, 2nd being 22 and so on).The input will be of the form of an ordered pair n,k where n<=9. Find the kth n-digit palindrome.Note: This problem is pretty elementary to such an expereinced person as you, and so the administration asks you to solve it as fast as possible, so try and minimize the running time of your code as much as possible.

Input

The first line of the input contains T, the number of test cases that follow. The following T lines contain two values separated by a space. The first one is n (number of digits in the palindrome) and the next is k (the kth number in ascending order to be found)

Output

The output should be of T lines, where each line has the corresponding output of the test case. That is line 2 will contain kth n-digit palindrome for the 2nd test case.

Example

Input:
3
4,9
7,50
9,120

Output:
1881
1049401
101191101

思路 打表可以发现 假如是9  第9个是   100080001 既 即中间的减一 第一个加1  如果是第52个 那么对应 可以先把数字变为 00052 然后第一个加1中间的减1
即100515001
对于8  那么也符合类似规律  那么只要先考虑前4个 比如 第99个 那么 先变成0099 然后 第一个加1 中间的减1 即10988901
就是这样的规律  拍出来即可 

#include<stdio.h>
#include<string.h>
int main()
{
    int n,cas,i,k;
    char s[15];
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d %d",&n,&k);
        if(n==9)
        {
            k=k-1;
             sprintf(s+1,"%05d",k);
             s[1]++;
             for(i=6;i<=9;i++)
                 s[i]=s[n-i+1];
             s[i]='\0';
             printf("%s\n",s+1);
        }
        else    if(n==7||n==8)            
        {
            k=k-1;
             sprintf(s+1,"%04d",k);
             s[1]++;
             for(i=5;i<=n;i++)
                 s[i]=s[n-i+1];
             s[i]='\0';
             printf("%s\n",s+1);
        }
        else if(n==5||n==6)
        {
             k=k-1;
             sprintf(s+1,"%03d",k);
             s[1]++;
             for(i=4;i<=n;i++)
                 s[i]=s[n-i+1];
             s[i]='\0';
             printf("%s\n",s+1);
        }
        else if(n==3||n==4)
        {
             k=k-1;
             sprintf(s+1,"%02d",k);
             s[1]++;
             for(i=3;i<=n;i++)
                 s[i]=s[n-i+1];
             s[i]='\0';
             printf("%s\n",s+1);
        }
        else if(n==2)
        {
              printf("%d\n",k*11);
        }
        else if(n==1)
        {
            printf("%d\n",k-1);
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值