poj2402

来源:互联网 发布:c语言和指针 编辑:程序博客网 时间:2024/05/29 15:49
Palindrome Numbers
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3950 Accepted: 1506

Description

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 first 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*10^9 ). 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 first 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

112240

Sample Output

133151
题意:就是找第几个数字回文串是什么。
思路:我们可以发现位数为1、2的都有9个回文数,3、4的有90个回文数,我们由此可以得到规律,然后先找到位数是几位,然后找每个位置的数字是啥。
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <cmath>using namespace std;#define ll long long intll a[105];void getnum(){    int i;    a[0]=0;    a[1]=1;    a[2]=1;    for(i=3;i<30;i++)    {       if(i%2==0)a[i]=a[i-1];       else a[i]=a[i-1]*10;    }}int main(){    ll n;    getnum();    while(scanf("%lld",&n)&&n)    {        int i,j;        int k=1;        while(n>a[k]*9)        {            n-=9*a[k++];        }        int mid=(k+1)>>1;        ll ans=1;        for(i=1,j=0;i<=mid;i++)        {            if(i==1)j=1;            else j=0;            while(n>a[k])            {                n-=a[k];                j++;            }            k-=2;            if(i==1)ans=j;            else ans=ans*10+j;        }        printf("%lld",ans);        if(k&1)ans/=10;        while(ans)        {            printf("%lld",ans%10);            ans/=10;        }        cout<<endl;    }    return 0;}


原创粉丝点击