POJ:http://poj.org/problem?id=2402

LA:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=890

题目大意:

回文数从小到大排列为:1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, ……输入n,(1<=n<=2*10^9),求第n小的回文数。

思路:

我们先统计一下i个数字组成的回文数的个数,cnt[i]=cnt[i-1]+9*mypow(10,p);

然后第n个回文数只要看看他是几位的就可以了。

如n=520,求出他需要5位,而1~4位所有的回文数个数(总的)为198,故k=520-198=322

找100为三位的基准,(为什么是3位? 长度为5的一般,向上取整。。为什么是100,三位最小哇)

100+k-1=100+321=421

所以一半求出来了,另一半呢(T T不是指对象)

根据对称性,可得42124

版本一:

#include<cstdio>
#include<cstring>
#include<cmath>
typedef long long LL;
const int MAXN=20;
LL cnt[MAXN]={0};
char ans[MAXN];
LL mypow(LL x,LL p)
{
long long res=1;
while(p--) res*=x;
return res;
}
int main()
{
for(int i=1;i<MAXN;i++)
{
LL p=(i+1)/2-1;
cnt[i]=cnt[i-1]+9*mypow(10,p);
} int n;
while(~scanf("%d",&n),n)
{
int k,num;
for(int i=1;i<MAXN;i++)
{
if(n <= cnt[i]) //万恶的等于号!!!!
{
k=n-cnt[i-1];//还需要的个数
num=i; //位数
break;
}
}
int len=num;
num=(num+1)/2;
LL x=mypow(10,num-1)+k-1; printf("%lld",x);
if(len %2!=0) x/=10;
while(x)
{
printf("%lld",x%10);
x/=10;
}
printf("\n");
}
return 0;
}

版本二:(请选择G++提交,因为pow函数要double而我是long long ,c++会说CP)

#include<cstdio>
#include<cmath>
#include<cstring>
typedef long long LL;
const int MAXN=22;
LL cnt[MAXN]={0};
char ans[MAXN];
int main()
{
for(int i=1;i<MAXN;i++)
{
LL p=(i+1)/2-1;
cnt[i]=cnt[i-1]+9*pow(10,p);
} int n;
while(~scanf("%d",&n),n)
{
int k,num;
for(int i=1;i<MAXN;i++)
{
if(n <= cnt[i])
{
k=n-cnt[i-1];
num=i;
break;
}
} LL x=pow(10,(num+1)/2-1);
x+=k-1;
sprintf(ans,"%lld",x);
int len=strlen(ans);
for(int i=0;i<len;i++)
printf("%c",ans[i]);
if(num &&num % 2==0)
printf("%c",ans[len-1]);
for(int i=len-2;i>=0;i--)
printf("%c",ans[i]);
printf("\n");
}
return 0;
}

POJ 2402 Palindrome Numbers(LA 2889) 回文数的更多相关文章

  1. POJ 3974 Palindrome(最长回文子串)

    题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...

  2. POJ 2402 Palindrome Numbers

    题目链接 水题,LA我居然没找到在那里. #include <cstdio> #include <cstring> #include <string> #inclu ...

  3. LeetCode第九题—— Palindrome Number(判断回文数)

    题目描述 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...

  4. POJ2402 Palindrome Numbers 回文数

    题目链接: http://poj.org/problem?id=2402 题目大意就是让你找到第n个回文数是什么. 第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是 ...

  5. POJ2402 Palindrome Numbers第K个回文数——找规律

    问题 给一个数k,给出第k个回文数  链接 题解 打表找规律,详见https://www.cnblogs.com/lfri/p/10459982.html,差别仅在于这里从1数起. AC代码 #inc ...

  6. leetcode 9 Palindrome Number 回文数

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

  7. [Swift]LeetCode479. 最大回文数乘积 | Largest Palindrome Product

    Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...

  8. leetcode-479-Largest Palindrome Product(找到两个乘数相乘得到的最大的回文数)

    题目描述: Find the largest palindrome made from the product of two n-digit numbers. Since the result cou ...

  9. 有趣的数-回文数(Palindrome number)

    文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...

随机推荐

  1. JDK版本切换批处理脚本

    我们经常在开发是遇到jdk版本切换的问题 1.手动去修改JAVA_HOME环境变量,将变量的值指向对应的JDK版本的安装目录即可. 2.通过编写批处理脚本来根据选择的JDK版本动态修改JAVA_HOM ...

  2. css3--简单的加载动画

    .load-container { width: 30%; height: auto; position: relative; margin: 1rem auto; } .load { width: ...

  3. 不固定高宽的 div 水平垂直居中

    <div class="father"> <div id="main"></div> </div> .fathe ...

  4. Es5正则

    ##JSON(ES5) 前端后台都能识别的数据.(一种数据储存格式) XNL在JSON出来前 JSON不支持  undefinde和函数. 示列:let = '[{"useername&qu ...

  5. python hmac 加密

    python2 :  key 是秘钥 类型为 str msg 要加密的文件 str digestmod 要加密的方式 python3: key 是秘钥 类型为 byte msg 要加密的文件 byte ...

  6. Flask框架简介

    Flask框架诞生于2010年,是Armin ronacher 用python语言基于Werkzeug工具箱编写的轻量级Web开发框架! Flask本身相当于一个内核,其他几乎所有的功能都要用到扩展. ...

  7. SVN无法提交,路径找不到问题的解决方案

    自己有了个云服务器,只在上面搭建了几个博客网站(小雷网等). 我自己的代码,保存在SVNChina上,每年99元.(私人的付费,开源的免费) 云服务器,闲着也是闲着,决定在上面搭建一个属于自己的SVN ...

  8. 高速入手ITOO导入-改进导入.xlsx格式

    这两天一直在研究师哥的导入,在他的基础上进行了一些改进.这次的改进就是能够导入还有一种格式.xlsx格式的.经过几番调试和看师哥写的底层代码最终知道哪里的问题了. workbook = new HSS ...

  9. GBX的Graph(最短路)

    Problem B: Graph Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 1  Solved: 1 [cid=1000&pid=1&am ...

  10. linux host主机名配置

    1.查看主机名 #hostname 2.查看ip #ifconfig 2.添加主机名配置 #vi /etc/hosts 新增一行 172.23.26.195 vhost145.idmp.safe