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
1
12
24
0
Sample Output
1
33
151
訓練的時候,有一個小步驟推錯了,然後就崩了。
然而這個題其實並不難。
AC代碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
#define ll long long int
int main()
{
int n;
while (~scanf("%d", &n) && n)//n位數
{
ll p = 9;
int i,t,k ,s[101000];//t=n/2+n%2
t = 1;
while (n > p)//9e(t-1) //計算此數在當前位數中的排行~ k的作用是去除前面的0的影響
{
t++;
n -= p;
if (t >= 3 && t % 2 == 1)//從奇數到偶數 任意改變的位數不變!
{
p *= 10;
}
}
int len = t / 2 + t % 2;//可以隨意改變的位數
k = p / 9;
n += k - 1;
for (int i = len; i >= 1; i--)//1/2,1/2的輸出
{
s[i] = n % 10;
n /= 10;
}
for (int i = 1; i <= len; i++)
cout << s[i];
if (t % 2) len--;
for (i = len; i >= 1; i--)
cout << s[i];
cout << endl;
}
return 0;
}