题目链接: 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. WSDL 文档解析

    学习webservice,就离不了WSDL文档,他是我们开发WebService的基础,虽说,现在现在有许多WebService的开源框架使得我们可以根据WSDL生成客户端代码,但是,了解WSDL文档 ...

  2. HtmlAgilityPack 简单运用

    WebClient client = new WebClient(); MemoryStream ms = new MemoryStream(client.DownloadData("htt ...

  3. 你真的知道css三种存在样式(外联样式、内部样式、内联样式)的区别吗?

    css样式在html中有三种存在形态: 内联样式:<div style="display: none"></div> 内部样式: <style> ...

  4. 学习笔记--Grunt、安装、图文详解

    学习笔记--Git安装.图文详解 安装Git成功后,现在安装Gruntjs,官网:http://gruntjs.com/ 一.安装node 参考node.js 安装.图文详解 (最新的node会自动安 ...

  5. Intellij 导入play framework 项目

    新建一个项目 play new helloworld IshallbeThatIshallbe:~ iamthat$ mkdir temp IshallbeThatIshallbe:~ iamthat ...

  6. cojs 疯狂的魔法树 疯狂的颜色序列 题解报告

    疯狂的魔法树 一个各种操作大杂烩的鬼畜数据结构题目 首先我们注意到树的形态是半随机的 我们可以树分块,对树分成若干个块 对于每个块我们维护一个add标记表示增量 维护一个vis标记表示覆盖量 注意标记 ...

  7. lintcode:最大子数组II

    题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 样例 给出数组[1, 3, -1, 2, -1, 2], ...

  8. Vimrc配置以及Vim的常用操作

    """"""""""""""""&quo ...

  9. *[topcoder]TheMatrix

    http://community.topcoder.com/stat?c=problem_statement&pm=13035 矩阵棋盘的题,比较典型.可以定两条column夹住,然后上下扫, ...

  10. HTML5 编辑 API 之 Range 对象(二)

    1.Range.cloneContents()The Range.cloneContents() returns a DocumentFragment copying the objects of t ...