POJ 2402 Palindrome Numbers(LA 2889) 回文数
POJ:http://poj.org/problem?id=2402
题目大意:
回文数从小到大排列为: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) 回文数的更多相关文章
- POJ 3974 Palindrome(最长回文子串)
题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...
- POJ 2402 Palindrome Numbers
题目链接 水题,LA我居然没找到在那里. #include <cstdio> #include <cstring> #include <string> #inclu ...
- LeetCode第九题—— Palindrome Number(判断回文数)
题目描述 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...
- POJ2402 Palindrome Numbers 回文数
题目链接: http://poj.org/problem?id=2402 题目大意就是让你找到第n个回文数是什么. 第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是 ...
- POJ2402 Palindrome Numbers第K个回文数——找规律
问题 给一个数k,给出第k个回文数 链接 题解 打表找规律,详见https://www.cnblogs.com/lfri/p/10459982.html,差别仅在于这里从1数起. AC代码 #inc ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [Swift]LeetCode479. 最大回文数乘积 | Largest Palindrome Product
Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...
- leetcode-479-Largest Palindrome Product(找到两个乘数相乘得到的最大的回文数)
题目描述: Find the largest palindrome made from the product of two n-digit numbers. Since the result cou ...
- 有趣的数-回文数(Palindrome number)
文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...
随机推荐
- headset-监听有线耳机插拔
今天在做项目的时候,需要对耳机的插拔事件进行监听,所以就写了如下的一个小demo,对耳机监听事件进行验证.直接看代码 package com.example.alert; import android ...
- 智课雅思词汇---二、词根acu和acr
智课雅思词汇---二.词根acu和acr 一.总结 一句话总结:acu和acr:sharp锋利的,敏捷的: acuteacutelyacuity sharp锋利的,敏捷的 1.词根acr表示什么意思? ...
- 利用css去除input按钮上的文字的几种方法
相信很多时候input上的文字困扰着web前端开发者,必须要通过修改html代码中的value值才能清空按钮上的文字,但很多人不愿意去动html代码,一方面麻烦,另外主要的原因还在于保留文字对seo有 ...
- 搜寻Linux软件实用指南
搜寻Linux软件实用指南 对于初学者来说,仅仅安装好Linux系统还是不够的,还需要安装大量的应用软件.许多下载网站都提供了诸如装机必备软件的下载,分门别类提供经典的工具软件下载.本文主要针对初学 ...
- IOS应用在ios7(iPhone5/iPhone5s)上不能全屏显示
前言 [IOS应用在iOS7系统或者iPhone5/iPhone5s上不能全屏显示,应用画面上下各有1条黑色,但是在其他系统或者型号的手机上却是正常显示 Paste_Image.png Paste_I ...
- javafx virtual keyboard
public class EffectTest extends Application { @Override public void start(final Stage stage) { final ...
- touch---创建文件或更改文件日期
- ubuntu 进入临时客户会话页面 转入用户自己页面
1.点击右上角的按钮 2.选择要登录的客户名字 点击进入 3.输入账号密码 进入指定的账号
- XML学习总结(2)——XML简单介绍
一.XML概念 Extensible Markup Language,翻译过来为可扩展标记语言.Xml技术是w3c组织发布的,目前推荐遵循的是W3C组织于2000发布的XML1.0规范. 二.学习XM ...
- CodeForces 159c String Manipulation 1.0
String Manipulation 1.0 Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on Cod ...