首页 > 代码库 > UVA - 12050-Palindrome Numbers

UVA - 12050-Palindrome Numbers

12050 - Palindrome Numbers

Time limit: 3.000 seconds 

A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, the name “anna” is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionally numbers can of course be ordered in size. The ?rst few palindrome numbers are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, ... The number 10 is not a palindrome (even though you could write it as 010) but a zero as leading digit is not allowed.
Input

The input consists of a series of lines with each line containing one integer value i (1 ≤ i ≤ 2?109). This integer value i indicates the index of the palindrome number that is to be written to the output, where index 1 stands for the ?rst palindrome number (1), index 2 stands for the second palindrome number (2) and so on. The input is terminated by a line containing ‘0’.
Output
For each line of input (except the last one) exactly one line of output containing a single (decimal) integer value is to be produced. For each input value i the i-th palindrome number is to be written to the output.
Sample Input
1

12

24

0
Sample Output
1

33

151

 

题意就是把第i个回文数输出来。

长度为k的回文数有9*10(k-1)个,依次计算,得出n为长度多少的回文数,然后得出长度多少的第几个回文数,n计算完要-1.

1-9 9

10-99 9

100-999 90

1000-9999 90

然后900 900 9000 9000。。。

 

傻子(╯‵□′)╯︵┻━┻,用C语言交了10几次,卡我格式错误,都不知道看一下用什么交的吗?(▼ヘ▼#)

代码:

#include<stdio.h>
const int N=2*1e5;
int n[N],i;
int h,m[N];
void gg(){
    n[0]=0,n[1]=n[2]=9;
    for(i=3;i<20;i+=2)
        n[i]=n[i+1]=n[i-1]*10;
}
int main(){
    gg();
    while(~scanf("%d",&h)&&h){
        int len=1;
        while(h>n[len]){
            h-=n[len];
            len++;
        }
        h--;
        int cnt=len/2+1;
        while(h!=0){
            m[cnt++]=h%10;
            h/=10;
        }
        for(i=cnt;i<=len;i++)
            m[i]=0;
            m[len]++;
        for(i=1;i<=len/2;i++){
            m[i]=m[len-i+1];
        }
        for(i=1;i<=len;i++)
            printf("%d",m[i]);
        printf("\n");
    }
    return 0;
}

 

一开始把gg函数(。。。)放在main函数里面了,不可以,不知道为啥。。。

不会。。。

 

UVA - 12050-Palindrome Numbers