码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 12050-Palindrome Numbers

时间:2017-07-03 22:25:31      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:sam   lin   rds   nsis   exce   this   --   integer   nat   

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

标签:sam   lin   rds   nsis   exce   this   --   integer   nat   

原文地址:http://www.cnblogs.com/ZERO-/p/7112991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!