UVALive 2889 Palindrome Numbers
2016-07-22 20:32
253 查看
Question:
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 ∗ 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 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
题意大意:将回文数从大到小排列,1,2,3,4,5,6,7,8,9,11,22,,,,99,,,输入一个数n,让你输出第n大的回文数是多少。
思路:仔细找你会发现一位数和两位数的回文数都只有9个,三位数和四位数的回文数有90个,五位数和六位数的回文数有900个,以此类推。
(http://acm.hust.edu.cn/vjudge/contest/121559#problem/H)
体会:做题要善于总结规律,发现规律,千万不要嫌麻烦,多算几组可能就发现规律了。
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 ∗ 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 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
题意大意:将回文数从大到小排列,1,2,3,4,5,6,7,8,9,11,22,,,,99,,,输入一个数n,让你输出第n大的回文数是多少。
思路:仔细找你会发现一位数和两位数的回文数都只有9个,三位数和四位数的回文数有90个,五位数和六位数的回文数有900个,以此类推。
(http://acm.hust.edu.cn/vjudge/contest/121559#problem/H)
#include <iostream> #include <cstdio> using namespace std; int main() { int n,i,j,t1,t2,t3; while (scanf("%d",&n),n) { i=9;j=1; while (1) { if(n<=i) { t1=j+n-1; //此部分数位奇数位,设位数为n,先算出前面部分n/2+1位 cout<<t1; t2=t1/10; while(t2) { cout<<t2%10; // 再算出后面的n/2位 t2/=10; } break; } n-=i; if(n<=i) { t1=j+n-1; //此部分数位偶数位,设位数为n,先算出前面部分n/2位 cout<<t1; t2=t1; while(t2) { cout<<t2%10; // 再算出后面的n/2位 t2/=10; } break; } n-=i; i*=10; j*=10; } cout<<endl; } return 0; }
体会:做题要善于总结规律,发现规律,千万不要嫌麻烦,多算几组可能就发现规律了。
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- C#递归算法之分而治之策略
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- C#算法之大牛生小牛的问题高效解决方法
- C#算法函数:获取一个字符串中的最大长度的数字
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法