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. jquery07

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  2. MyEclipse下怎么配置Maven

    这个很简单. 前期博客,请移步: Eclipse下Maven新建项目.自动打依赖jar包(包含普通项目和Web项目) 截图示范:

  3. OA项目笔记

    一.创建项目构架 1.创建一个Maven的web工程 1.1修改编译器版本 <properties> <project.build.sourceEncoding>UTF-8&l ...

  4. Python day4知识回顾

    # -*- coding: utf_8 _*_# Author:Vi#字典是无序的 info = { 'student001':"DIO", 'student002':" ...

  5. Java中join和yield的作用

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 1.   A.join,在API中的解释是,堵塞当前线程B,直到A执行完毕并死掉,再执行B. 用一个 ...

  6. 洛谷 P2782 友好城市

    P2782 友好城市 题目描述 有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市.北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同.每对友好城市都向政府申 ...

  7. golang 简单web服务

    1.golang print输入 package main import "fmt" func main() { fmt.Printf("Hello World!\n&q ...

  8. hive load文件第一个字段为NULL

    在hive中,通常须要载入外部数据源.load文件时.第一个字段会出现NULL. 比如: 1.运行load语句: LOAD DATA LOCAL INPATH 'test.txt' OVERWRITE ...

  9. 在Google Drive上建立免费静态站点

    现今建立一个属于自己的站点已经是一件非常普遍和简单的事情了. 你能够选择买空间,买域名.你也能够使用免费空间.免费域名.你能够选择动态的php wordpress,joomla或者是静态的站点(如使用 ...

  10. Flume的可扩展性

    Flume的可扩展性:Flume采用了三层架构,分别为agent,collector和storage,每一层均可以水平扩展.其中,所有agent和 collector由master统一管理,这使得系统 ...