首页 > 代码库 > UVA - 12050 Palindrome Numbers
UVA - 12050 Palindrome Numbers
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 as010) but a zero as leading digit is not allowed.
Input
The input consists of a series of lines with each line containing one integer valuei (1 <= i <= 2*109). This integer value i indicates the index of the palindrome number that is to be written to the output, where index1 stands for the first palindrome number (1), index2 stands for the second palindrome number (2) and so on. The input is terminated by a line containing0.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 valuei the i-th palindrome number is to be written to the output.Sample Input
1 12 24 0
Sample Output
1 33 151题意:求第n个回文串思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然后就得到是长度为多少的第几个的回文串了,有个细节注意的是,n计算完后要-1,啊,起初没这么做,有BUG,后面再加回来#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> typedef long long ll; using namespace std; const int maxn = 3000; ll num[maxn]; int n, ans[maxn]; void init() { num[0] = 0, num[1] = num[2] = 9; for (int i = 3; i < 20; i += 2) num[i] = num[i+1] = num[i-1] * 10; } int main() { init(); while (scanf("%d", &n) && n) { int len = 1; while (n > num[len]) { n -= num[len]; len++; } n--; int cnt = len / 2 + 1; while (n) { ans[cnt++] = n % 10; n /= 10; } for (int i = cnt; i <= len; i++) ans[i] = 0; ans[len]++; for (int i = 1; i <= len/2; i++) ans[i] = ans[len-i+1]; for (int i = 1; i <= len; i++) printf("%d", ans[i]); printf("\n"); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。