Dhaka 2003 / UVa 12050 - Palindrome Numbers (回文数)
来源:互联网 发布:淘宝签到怎么补签到 编辑:程序博客网 时间:2024/05/05 22:47
12050 - Palindrome Numbers
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=3202
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
112240
Sample Output
133151
打表,先根据表找出这个回文数有多少位,然后算一般,另一半照前面的输出。
完整代码:
/*0.015s*/#include<cstdio>#include<cstring>#include<cstdlib>const int endsum[21] ={0, 9, 18, 108, 198, 1098, 1998, 10998, 19998, 109998, 199998, 1099998, 1999998,10999998, 19999998, 109999998, 199999998, 1099999998, 1999999998, 2000000001};char str[20];int main(void){int len, num, i, j, maxj;int ans;while (gets(str), str[0] & 15){len = strlen(str);num = atoi(str);if (len == 1)putchar(str[0]);else if (num < 19){num += 39;putchar(num);putchar(num);}else{i = 2;while (endsum[++i] < num);/// 此时回文数的位数为inum -= endsum[i - 1] + 1; /// 多减一个/// 现在num是第num个i位数的回文数ans = 1;maxj = (i + 1) >> 1; /// i/2上取整for (j = 1; j < maxj; ++j)ans *= 10;ans += num;sprintf(str, "%d", ans);for (j = 0; j < maxj; ++j)putchar(str[j]);if ((i & 1) == 0)///优先级啊。。putchar(str[maxj - 1]);for (j = maxj - 2; j >= 0; --j)putchar(str[j]);}putchar('\n');}return 0;}
- Dhaka 2003 / UVa 12050 - Palindrome Numbers (回文数)
- uva 12050 - Palindrome Numbers
- UVa:12050 Palindrome Numbers
- UVA - 12050 Palindrome Numbers
- uva 12050 - Palindrome Numbers(数论)
- POJ 2402 Palindrome Numbers(LA 2889) 回文数
- Palindrome Numbers(LA2889)第n个回文数是?
- UVALive2889 Palindrome Numbers(回文数,神级代码....)
- UVA 12050 - Palindrome Numbers(数论+规律)
- POJ1159:Palindrome(回文数)
- LeetCode Palindrome Number回文数
- Leetcode-- Palindrome Number 回文数
- palindrome-number(回文数)
- LeetCode 9 palindrome (回文数)
- 9. Palindrome Number(回文数)
- Palindrome Function(回文数)
- Palindrome Number判断回文数
- leetcode Palindrome Number 回文数
- 简单逆向分析修改软件标题
- hdu 3652 -数位dp
- 将“引用”作为函数返回值类型
- java、javaweb 日志技术汇总(log4j , Commons-logging,.....)
- C++编译结果窗口一闪而过的问题
- Dhaka 2003 / UVa 12050 - Palindrome Numbers (回文数)
- SICP 习题 (1.15) 解题总结
- Java NIO系列教程(二) Channel
- 黑马程序员------面向对象(No.4)(对象初始化过程、单例、继承概述、重写)
- window7 Eclipse中远程开发Hadoop
- Java NIO系列教程(三) Buffer
- 让人感悟的几个图片
- 邮件发送工具类(自认为可以了)
- hdu 4709