POJ 2402 Palindrome Numbers

POJ2402 Palindrome Numbers 回文数 题目链接: http://poj.org/problem?id=2402 题目大意就是让你找到第n个回文数是什么. 第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是这样的思路). 很明显找到第n个之前的所有操作都是浪费, 这也是这个方法的最大弱点. 抱着侥幸心理(谁知道数据弱不弱啊)用这种方法提交了下, TLE (在另一个OJ上提交是9个测试点过了6个). ... 阅读详情
Palindrome Numbers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3943 Accepted: 1502

Description

A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, the name "anna" is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionally numbers can of course be ordered in size. The first few palindrome 
numbers are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, ... 
The number 10 is not a palindrome (even though you could write it as 010) but a zero as leading digit is not allowed.

Input

The input consists of a series of lines with each line containing one integer value i (1<= i <= 2*10^9 ). This integer value i indicates the index of the palindrome number that is to be written to the output, where index 1 stands for the first palindrome number (1), index 2 stands for the second palindrome number (2) and so on. The input is terminated by a line containing 0.

Output

For each line of input (except the last one) exactly one line of output containing a single (decimal) integer value is to be produced. For each input value i the i-th palindrome number is to be written to the output.

Sample Input

1
12
24
0

Sample Output

1
33
151

Source

题意:

输出第n个回文数字。


POINT:

一直用搜索做,做不出。

先求出第n个的长度,然后截一半,从10**0开始数,第k个就是答案。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
#define  LL long long
LL a[20];
void init()
{
    a[1]=9;
    a[2]=9;
    for(int i=3;;i++)
    {
        a[i]=a[i-2]*10;
        if(a[i]>=2*1e9) break;
    }
}
int main()
{
    LL n;
    init();
    while(~scanf("%lld",&n)&&n)
    {
        LL now=0;
        int i;
        for(i=1;;i++)
        {
            now+=a[i];
            if(now>=n)
                break;
        }
        int k=i;
        n=n-now+a[i];
        int l;
        if(k&1) l=k/2+1;
        else l=k/2;
        now=(LL)pow(10,l-1);
        LL ans=now+n-1;
        printf("%lld",ans);
        int anss[20];
        for(int i=1;i<=l;i++)
        {
            anss[i]=ans%10;
            ans/=10;
        }
        if(k&1) i=2;
        else i=1;
        for(;i<=l;i++)
        {
            printf("%d",anss[i]);
        }
        printf("\n");
        
    }
    
}


poj2402(计算第k个回文数) 题目链接:poj2402 /* 题意: 求第n个回文数 思路: 不难发现规律,1位和2位的回文数有9个,3位和4位的回文数有90个。。。。 首先求出第k位的基数 10^((k-1)/2),再计算出第n个回文数是几位数, 对半后,对前半段进行填数,后半段的只要将前半段反向输出即可 */ #include #include #include #include using n 阅读详情

相关推荐

poj 2402

数位统计问题,求出第K个回文数字。定义dp[i][j]表示i位数字以j作为开始和结尾的回文数字个数,则dp[i][j]+=dp[i-2][k](k=0-9)。 然后从高位开始一次确定每一位的数字。一些中间变量要超long long。 #include #include #include #include #include using namespace std; const int inf=2

suressay的专栏 720

POJ 2402 Palindrome Numbers(LA 2889) 回文数

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

weixin_30340353的博客 176

POJ 2402 Palindrome Numbers 笔记

对所有数字组成的回文编号,单个数字也是回文。求编号为 i 的回文。

woniupengpeng的专栏 342

poj 2402 Palindrome Numbers 数字回文 (★★☆☆☆)

http://poj.org/problem?id=2402 题意:求从1开始的第i(1<= i <= 2*10^9)个数字回文。 由于给出的测试数据范围太大,暴力搜索不得不丢弃。在网上找了两篇解题报告,看了好一会,才完全弄明白。 解题方案: (1)先求出第i个数字回文的位数k,当位数为k时,k位数字组成的回文数为 f(k)=9*10^((k-1)/2),可以根...

weixin_34211761的博客 287

poj 2402 Palindrome Numbers

<br />从1开始,求第k个回文数。<br />首先,手算x位上的回文数个数为f(x)=9*10^((x+1)/2);<br />由此我们可以算出第k个回文数的位数,假设为y。<br />另tmp=k-sum{f(x)}  ,1<=x<k<br />则可以求出那个y位的“基数”,例如 5位数abcde。。则基数为100;<br />基数加上tmp.凑成half。然后再对称一下就求出第k个回文数<br /> <br />// poj 2402 #include<iostream> #include<c

spirit9007的专栏 353

E - Palindrome Numbers POJ - 2402

A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, the name “anna” is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionally num

Irish_Moonshine的博客 426

POJ-2402Palindrome Numbers

【题目描述】 求第n个回文数 【解题思路】 首先求出一共k位长的数一共多少个回文,然后对半开 /* list[i]表示位数为i的满足回文数要求共有list[i]个 */ ll List[20] = {0, 9, 9, 90, 90, 900, 900, 9000, 9000, 90000, 90000, 900000, 900000, 9000000, 9000000, 90000000,

famousDT的专栏 965

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

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

dianshu1593的博客 195

POJ2402/UVA 12050 Palindrome Numbers 数学思维

A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the name “anna” is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionallynumb...

weixin_30548917的博客 143

Uva 12050 (POJ 2402) - Palindrome Numbers 解题报告(数学)

A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, the name "anna" is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionally n

SF-_- 的 ACM 博客 1316

POJ2402+模拟

题意:找出第index 个回文数。 这题是有规律的,即list[]数组。 其次,对于某个 index 可以先精确到 位数 pos,然后在进行分析。 1a 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 typedef __int64 int...

weixin_30889885的博客 137

poj2402 kmp算法

终于自己的思路过了一道题。。。。。 #include #include #include] #include #include #include #include #include #include #include using namespace std; #define ll __int64 #define rep(i,n) for(i=0; i<(n); i++) #define rep

生活总是要微笑着面对~ 791
上一篇: HDU 6069 Counting Divisors(求因子)
下一篇: HDU 6053 TrickGCD(枚举)
Mr_Treeeee
博客等级 码龄9年 38粉丝 551原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值