还是费了一点时间,,
提示::先是计算出n位数对应的回数的个数.
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
long long i,j;
int n;
long long go[10000];
long long k;
go[1]=9;
for (i=2;i<50;i++)
{
if (i&1)
{
k=9;
j=i/2+1;
while (--j)
k*=10;
go[i]=go[i-1]+k;
}
else
{
k=9;
j=i/2;
while (--j)
k*=10;
go[i]=go[i-1]+k;
}
//cout<<go[i]<<endl;
}
go[0]=0;
while (cin>>n,n)
{
for (i=1;i<50;i++)
{
if (n<=go[i])
{
n-=go[i-1];
//cout<<n<<endl;
if (i&1)
{
j=i/2;
k=1;
while (j--)
k*=10;
k+=n-1;
cout<<k;
k/=10;
while (k)
{
cout<<k%10;
k/=10;
}
cout<<endl;
}
else
{
j=i/2;
k=1;
while (--j)
k*=10;
k+=n-1;
cout<<k;
while (k)
{
cout<<k%10;
k/=10;
}
cout<<endl;
}
break;
}
}
}
return 0;
}
Palindrome Numbers
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3336 | Accepted: 1255 |
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.
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
1 12 24 0
Sample Output
1 33 151
Source