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

来源:互联网 发布:网络好电视猫最近很卡 编辑:程序博客网 时间:2024/05/01 14:22

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

Palindrome Magic

Problem code: T02

  • Submit
  • My Submissions
  • All Submissions

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:34,97,509,120Output:18811049401101191101思路 打表可以发现 假如是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;}




原创粉丝点击