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. jquery的ajax总结

    jquery的ajax总结 一.总结 一句话总结:ajax函数中层级关系如下: 最底层的封装方式: $.ajax(); 第二层: .load(),$.get(), $.post() 最高层: $.ge ...

  2. Android官方文档翻译——Fragment生命周期

    网上有的博客写得太乱 不如自己翻译官方文档 Lifecycle 生命周期 Though a Fragment's lifecycle is tied to its owning activity, i ...

  3. Redis安装以及配置

    下载 http://redis.io/download 解压 tar zxvf redis-2.8.17.tar.gz 编译并安装 1 2 3 4 cd redis-2.8.17 make cd sr ...

  4. Android 如何获取Android RecyclerView滑动的距离

    如何获取 RecyclerView 的滑动距离? RecyclerView 虽然有getScrollX() 和 getScrollY(), 但是测试发现这两个函数总是返回0,太无语了.因此想到了下面几 ...

  5. Codefroces Educational Round 26 837 B. Flag of Berland

    B. Flag of Berland time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. log大全

    http://www.iconfont.cn/search/index?q=%E6%88%91%E7%9A%84&page=3

  7. 【习题 8-9 1613】 K-Graph Oddity

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 感觉最大度数|1就是最多需要的个数了. 就贪心一下. 然后模拟染色的过程就可以了. (贪心染色就可以了 (看看周围哪个颜色没有,就用 ...

  8. 简单的WINFORM窗口,体验WINFORM带来的快感

    当习惯成为一种自然,就不再喜欢那种条条框框的规则 using System; using System.Windows.Forms; namespace Window{ class Window{ s ...

  9. Linux 获取上个月的第一秒和上个月的最后一秒

    因为写脚本需求须要获得上个月的第一秒和上个月的最后一秒,查阅了相关资料,并通过自己实践.找到了以下这样的方法能满足要求.在此备注,若有其它好的方法.请留言.本人将不胜感激. 获取上个月的第一秒: da ...

  10. 高速数论变换(NTT)

    今天的A题.裸的ntt,但我不会,于是白送了50分. 于是跑来学一下ntt. 题面非常easy.就懒得贴了,那不是我要说的重点. 重点是NTT,也称高速数论变换. 在非常多问题中,我们可能会遇到在模意 ...