题目链接: http://poj.org/problem?id=2402

题目大意就是让你找到第n个回文数是什么.

第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是这样的思路). 很明显找到第n个之前的所有操作都是浪费, 这也是这个方法的最大弱点. 抱着侥幸心理(谁知道数据弱不弱啊)用这种方法提交了下, TLE (在另一个OJ上提交是9个测试点过了6个).

第二个思路也很容易想到, 但是比第一个思路要麻烦: 第n个回文数的每位数字都与n有一定的关联. 也就是说由n的值可以推测出回文数. 那么如何推算呢? 首先当然要来找规律. 我们可以发现:

位数为1和2的回文数有9个 (1-9, 11-99)

位数为3和4的回文数有90个 ({101-191, 202-292, ...}, {1001-1991, 2002-2991, ...})

位数为5和6的回文数有900个 (不再列举)

......

所以:

1-9个回文数的位数为1

10-18个回文数的位数为2

......

然后确定了位数之后(从某种意义上说)n已经没有用了, 此时有用的应该是n在这个区间的位置.

比如第11个回文数是2位数, 11在10-18这个区间中是第2个. 2位数的回文数是从11-99, 而11在10-18中是第2个, 所以是11-99中的第2个, 也就是22.

然后把n的区间拉到19-98, 也就是说回文数是3位数的时候. 从19-28, 也就是这个区间当中的第1-10个, 它们的第1位和第3位都是1(1-10是这个区间中的第1组10个数); 从29-38(区间中的第11-20个), 它们的第1位和第3位都是2(11-20是这个区间中的第2组10个数); 以此类推.

继续看19-28(区间中的第1-10个), 它们在区间中的序号就是回文数的第二位.

......

所以, 就可以发现区间决定一切. 回文数的每一位, 都由n所处的区间所决定.

到这里大概就可以写出程序了. 有些细节可能需要处理. 下面上代码.

 #include <iostream>
using namespace std; char toch(int n) {
return n+'';
} long long power(int e) {
int sum = , i = ;
for (; i<e; i++)
sum *= ;
return sum;
} void gen(long long n, char* s) {
long long i, lvl = , w, t, div;
for (i=; ; i++) {
t = * power(i/);
if (n <= lvl+t) {
w = i+;
n -= lvl;
break;
}
lvl += t;
}
n--;
div = power((w-)/);
for (i=; i<(w+)/; i++) {
s[i] = s[w-i-] = toch(w< ? n/div+ : (i?n/div:n/div+));
n %= div;
div /= ;
}
} int main() {
long long t;
while () {
cin >> t;
if (!t)
break;
char s[] = {};
gen(t, s);
cout << s << endl;
}
return ;
}

//其实没必要long long, 这里只是为了保险起见.

POJ2402 Palindrome Numbers 回文数的更多相关文章

  1. UVa 12050 - Palindrome Numbers (回文数)

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, th ...

  2. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  3. Leetcode 3——Palindrome Number(回文数)

    Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...

  4. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  5. LeetCode Problem 9:Palindrome Number回文数

    描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  6. [LeetCode]9. Palindrome Number回文数

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  7. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  8. Palindrome Number 回文数

    判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出.     ...

  9. 【LeetCode】Palindrome Number(回文数)

    这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...

随机推荐

  1. 【UVA】【11021】麻球繁衍

    数序期望 刘汝佳老师的白书上的例题……参见白书 //UVA 11021 #include<cmath> #include<cstdio> #define rep(i,n) fo ...

  2. [CFgym]2015-2016 ACM-ICPC Pacific Northwest Regional Contest小结

    *感谢两位浙江大佬带我飞 贴下成绩 div2 div1 *div2不是我打的上个厕所就5/11了 比赛小结 A [题目大意] 有n(n<=500)个机场,两两之间距离是g[i][j],每经停一个 ...

  3. 2013 Asia Regional Changchun

    Hard Code http://acm.hdu.edu.cn/showproblem.php?pid=4813 #include<cstdio> ]; int main(){ int t ...

  4. mac上eclipse上配置hadoop

    在mac上安装了eclipse之后,配置hadoop其实跟在linux上配置差不多,只是mac上得eclipse和界面和linux上得有点不同. 一:安装eclipse eclipse得安装比较简单, ...

  5. [shell编程]正则表达式

    如果在shell脚本中处理数据文件,那么我们就必须熟悉正则表达式.正则表达式是用来过滤数据流中文本的模式模板,模式由标准文本字符和特殊字符组成.正则表达式用特殊字符来匹配一系列一个或多个字符,要想掌握 ...

  6. javascript中li标签的排序和数组sort的用法

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  7. POJ1442Black Box

    http://poj.org/problem?id=1442 题意 : 题目中对给出的数字有两种操作ADD(I)操作,将ADD括号里的数字 I 加到数列里边去,然后是自动排好序的,每一个数列前边都会有 ...

  8. linux下用非root用户重启导致ssh无法连接的问题

    问题描述 安装好了centOS服务器,一直用Secure CRT工具通过ssh服务来远程连接linux,很方便的进行各种操作.今天偶然尝试了一下在非root的一般用户下执行重启服务器的命令,发现一般用 ...

  9. iOS开发--使用NSMutableAttributedString 实现富文本

    在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦 ...

  10. 223. Rectangle Area

    题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...