题目链接: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. PythonPath在Windows 下的设置

    今天在调试Evernote SDK时, 遇到PythonPath的问题. 查了很多资料,有说用系统环境变量添加PythonPath, 有说在注册表中的PythonPath添加新Default字段, 但 ...

  2. asp.net分页asp.net无刷新分页高效率分页

    项目中经常会用到分页的功能类似的项目做过无数个了,今个把自己常用的分页代码分享一下. 首先说说服务端处理的代码: 下面代码中重点是分页的sql语句的写法,其中的参数@n是当前的页码,总的来说本服务端主 ...

  3. for循环中的条件执行循序

    问题: public class Main { public static void main(String[] args) { int i,n,length = 0; for(i=1;length& ...

  4. Centos开启telnet/ssh/ftp/sftp服务

    Telnet 开启telnet服务步骤: 1.   查看CentOS/Telnet_server版本:#cat /etc/issue,   #rpm -qa | grep telnet 2.   安装 ...

  5. python os.startfile python实现双击运行程序 python监控windows程序 监控进程不在时重新启动

    用python监控您的window服务 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://world77.blog.51cto.co ...

  6. winform 客户端 HTTP协议与服务端通信以及解决中文乱码

    本来从来没有仔细研究过Http协议,今天因为公司业务需求,调试了半天,终于现在会Winform用Http协议与服务端通信了,其中常用的有POST和Get方式: 仔细看了人人网和新浪等大部分都是采用GE ...

  7. vue.js 使用高德地图

    1.获取key值 注册成为高德开发者需要分三步: 第一步,注册高德开发者: 第二步,去控制台创建应用: 第三步,获取Key 2.修改配置文件  webpack.base.conf.js externa ...

  8. Sequence Models 笔记(二)

    2 Natural Language Processing & Word Embeddings 2.1 Word Representation(单词表达) vocabulary,每个单词可以使 ...

  9. .clearfix:after

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Python最小二乘法解非线性超定方程组

    求解非线性超定方程组,网上搜到的大多是线性方程组的最小二乘解法,对于非线性方程组无济于事. 这里分享一种方法:SciPy库的scipy.optimize.leastsq函数. import numpy ...