题目链接:https://vjudge.net/contest/237394#problem/E

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∗109). 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

题目大意:输入n,求第n个回文数,从1开始

个人思路:这题要先找规律,可以发现增长关系是9,9,90,90,900,900····一直下去,这样就可以把要求的数所在的小范围区间求出来,求该数是这个范围内第几个数,然后求出来就行

看代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e8+;
const int maxk=+;
const int maxx=1e4+;
const ll maxa=;
#define INF 0x3f3f3f3f3f3f
ll a[];
ll ans[];
void solve(ll n,ll W)
{
int cnt=W;
// cout<<n<<" "<<W<<endl;
ans[cnt--]=n%-;//最后一位是从0开始的,所以要减1,但是这里要注意,n%10可能为0,为0的话其实就是上一位减1,这一位加10
n=n/;//同时n减少一位
if(ans[W]<)
{
ans[W]=ans[W]+;
n--;
}
while(n)
{
ans[cnt--]=n%;
n/=;
}
ans[]++;//注意第一位从1开始的,要++
}
int main()
{
ios::sync_with_stdio(false);
ll sum=,sum1=,P;
for(int i=;i<=;i+=)
{
a[i]=a[i+]=*sum;
sum*=;
sum1+=a[i]*;
if(sum1>=*pow(,))
{
P=i;
break;
}
}
ll n;
a[]=;
while(cin>>n)
{
memset(ans,,sizeof(ans));
sum=;
int W;
if(n==)
break;
if(n>&&n<)
{
cout<<n<<endl;
continue;
}
for(int i=;i<=P;i++)
{
sum+=a[i];
if(n<=sum)
{
W=i;
sum-=a[i];
n-=sum;
break;
}
}
//cout<<W<<endl;
if(W%==)
{
W=W/;
solve(n,W);
for(int i=;i<=W;i++)
cout<<ans[i];
for(int i=W;i>=;i--)
cout<<ans[i];
}
else
{
W=(W+)/;
solve(n,W);
for(int i=;i<=W;i++)
cout<<ans[i]; for(int i=W-;i>=;i--)
cout<<ans[i];
}
cout<<endl;
// cout<<n<<endl;
//cout<<ans<<endl;
}
return ;
}

E - Palindrome Numbers的更多相关文章

  1. POJ2402/UVA 12050 Palindrome Numbers 数学思维

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...

  2. Palindrome Numbers(LA2889)第n个回文数是?

     J - Palindrome Numbers Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu ...

  3. Uva - 12050 Palindrome Numbers【数论】

    题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...

  4. UVa 12050 - Palindrome Numbers (回文数)

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, th ...

  5. Leetcode: Palindrome Numbers

    Determine whether an integer is a palindrome. Do this without extra space. 尝试用两头分别比较的方法,结果发现无法解决1000 ...

  6. POJ 2402 Palindrome Numbers

    题目链接 水题,LA我居然没找到在那里. #include <cstdio> #include <cstring> #include <string> #inclu ...

  7. UVA 12050 - Palindrome Numbers 模拟

    题目大意:给出i,输出第i个镜像数,不能有前导0. 题解:从外层开始模拟 #include <stdio.h> int p(int x) { int sum, i; ;i<=x;i+ ...

  8. LA 2889 (找规律) Palindrome Numbers

    输出第n个回文数. 规律就是一位和两位的回文数各有9个,三位四位的回文数各有90个,以此类推. 给出n,可以先判定一下第n个回文数的位数,然后后面也不难推,但是有很多细节需要注意. #include ...

  9. POJ2402 Palindrome Numbers 回文数

    题目链接: http://poj.org/problem?id=2402 题目大意就是让你找到第n个回文数是什么. 第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是 ...

随机推荐

  1. rsync 介绍和参数说明

    Rsync 介绍: 我们经常需要在不同目录或者服务器之间做文件同步和更新,Linux提供了很多内置命令可以使用比如scp等等,但是今天我们介绍一个更加强大的工具rsync.rsync 命令是一个远程同 ...

  2. 动态webService

    using System; using System.Net; using System.IO; using System.CodeDom; using Microsoft.CSharp; using ...

  3. css之布局

    布局一直是页面制作很重要的部分,有个良好的布局不仅在页面上呈现很好的效果,还对后续功能扩展有重要的作用.本文主要讨论一下几种布局: 水平居中布局 垂直居中布局 多列布局 自适应布局 stracky-f ...

  4. 使用struts2进行文件下载以及下载权限控制的例子

    本测试有两个模块,一个是文件上上传,一个是文件下载,文件下载的时候会检查是否足有权限,如果没有,就会转发到登录页面,如果有权限,就会直接启动下载程序,给浏览器一个输出流. 下面直接上我的代码: 登录表 ...

  5. 由hibernate配置inverse="true"而导致的软件错误,并分析解决此问题的过程

    题目背景软件是用来做安装部署的工具,在部署一套系统时会有很多安装包,通过此工具,可以生成一个xml文件用以保存每个安装包的文件位置.顺序.参数.所需脚本.依赖条件验证(OS..net.IIS.数据版本 ...

  6. Hibernate学习第一课

    Hibernate是一个框架 一个Java领域的持久化框架 一个ORM框架 对象的持久化: 狭义的理解:“持久化”仅仅指把对象永久保存到数据库中. 广义的理解:“持久化”包括和数据库相关的各种操作: ...

  7. 面试题18(一):在O(1)时间删除链表结点

    // 面试题18(一):在O(1)时间删除链表结点 // 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该 // 结点.链表结点与函数的定义如下: // struct Lis ...

  8. Object—C 块在函数中作为参数时的分析

    暂时对这个有了一些粗浅的理解,记下来一边后面学习时学习,改正. 先举个例子: A类: .h文件: @interface A  : NSObject - (void)Paly1:(void (^)(do ...

  9. 理解linux服务器mcelog如何工作

    What are Machine Check Exceptions (or MCE)? A machine check exception is an error dedected by your s ...

  10. C/C++中的static用法总结

    C中: 1. static修饰函数中的变量(栈变量):改变变量的生存期,作用域不变仍为所在函数. 只被初始化一次. 2. static修饰全局变量:限制全局变量只能被模块内访问,不可以在别的模块中用e ...