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. JAVA基础针对自己薄弱环节总结02(循环)

    循环 A:水仙花. classShuiXianHua { public static void main(String[] args) { for(int i=101;i<1000;i++) { ...

  2. php数组合并有哪三种方法

    php数组合并有哪三种方法 一.总结 一句话总结:array_merge():array_merge_recursive():‘+'号 $a = array('color'=>'red',5,6 ...

  3. Linux智能手机安全策略研究

    Linux智能手机安全策略研究 http://www.zdnet.com.cn    本文是继从“窃听门”事件解读手机Rootkit攻击(http://chenguang.blog.51cto.com ...

  4. 搜寻Linux软件实用指南

    搜寻Linux软件实用指南  对于初学者来说,仅仅安装好Linux系统还是不够的,还需要安装大量的应用软件.许多下载网站都提供了诸如装机必备软件的下载,分门别类提供经典的工具软件下载.本文主要针对初学 ...

  5. 在电子商务里,一般会提到这样几个词:商品、单品、SPU、SKU

    简单理解一下,SPU是标准化产品单元,区分品种:SKU是库存量单位,区分单品:商品特指与商家有关的商品,可对应多个SKU. 首先,搞清楚商品与单品的区别.例如,iphone是一个单品,但是在淘宝上当很 ...

  6. layui动态无限极菜单

    ajax加jQuery实现 效果图 参考文章:https://www.wanpishe.top/detail?blogId=644aa177-9795-456a-8090-ee1264bf5d9d

  7. ubuntu 进入临时客户会话页面 转入用户自己页面

    1.点击右上角的按钮 2.选择要登录的客户名字 点击进入 3.输入账号密码 进入指定的账号

  8. Nim游戏算法实现

  9. Highcharts柱形范围图使用示例

    功能需求:统计三种不同的状态在一天的时间段里面所占的范围 第一步:引入highcharts.js和highcharts-more.js文件 引入文件文件源码:下载https://img.hcharts ...

  10. STM32之串口IAP更新升级

    一.IAP简介 IAP是应用编程,目的是为了在产品发布后可以方便地通过预留的通信口对产品中的固件程序进行更新升级,后续产品发布后,更新程序我只需要把.bin文件通过串口发送给芯片就可以执行更 新,很方 ...