题目链接: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+7;
const int maxn=1e8+10;
const int maxk=100+10;
const int maxx=1e4+10;
const ll maxa=2520;
#define INF 0x3f3f3f3f3f3f
ll a[50];
ll ans[25];
void solve(ll n,ll W)
{int cnt=W;//  cout<<n<<" "<<W<<endl;ans[cnt--]=n%10-1;//最后一位是从0开始的,所以要减1,但是这里要注意,n%10可能为0,为0的话其实就是上一位减1,这一位加10n=n/10;//同时n减少一位if(ans[W]<0){ans[W]=ans[W]+10;n--;}while(n){ans[cnt--]=n%10;n/=10;}ans[1]++;//注意第一位从1开始的,要++
}
int main()
{ios::sync_with_stdio(false);ll sum=1,sum1=0,P;for(int i=1;i<=50;i+=2){a[i]=a[i+1]=9*sum;sum*=10;sum1+=a[i]*2;if(sum1>=2*pow(10,9)){P=i;break;}}ll n;a[0]=0;while(cin>>n){memset(ans,0,sizeof(ans));sum=0;int W;if(n==0)break;if(n>0&&n<10){cout<<n<<endl;continue;}for(int i=1;i<=P;i++){sum+=a[i];if(n<=sum){W=i;sum-=a[i];n-=sum;break;}}//cout<<W<<endl;if(W%2==0){W=W/2;solve(n,W);for(int i=1;i<=W;i++)cout<<ans[i];for(int i=W;i>=1;i--)cout<<ans[i];}else{W=(W+1)/2;solve(n,W);for(int i=1;i<=W;i++)cout<<ans[i];for(int i=W-1;i>=1;i--)cout<<ans[i];}cout<<endl;// cout<<n<<endl;//cout<<ans<<endl;
    }return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/9370170.html

E - Palindrome Numbers相关推荐

  1. Uva - 12050 Palindrome Numbers【数论】

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

  2. Leetcode: Palindrome Numbers

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

  3. POJ2402 Palindrome Numbers 回文数

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

  4. 北林oj-算法设计与分析-Tom palindrome number

    描述 Tom is studing math these days. If there is a number X, whose binary form and decimal form are al ...

  5. 训练指南第二章-基础问题

    训练指南第二章-基础问题 P170 2 / 4 Problem A UVA 10943 How do you add? 1 / 2 Problem B UVA 10780 Again Prime? N ...

  6. 《算法入门经典大赛——培训指南》第二章考试

    UVa特别考试 UVa站点专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge& ...

  7. 思考题目,仔细检查,外加一个ceil函数

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

  8. POJ的题目分类(两个版本)

    版本一: 简单题 1000A+B Problem 1001Exponentiation 1003 Hangover 1004 Financial Management 1005 I Think I N ...

  9. POJ前面的题目算法思路【转】

    1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...

最新文章

  1. debian10 Unable to correct problems, you have held broken packages.
  2. 3、顺序表、内存、类型、python中的list
  3. PXE网络引导装机原理介绍
  4. [蓝桥杯2016决赛]阶乘位数-数论
  5. Java命令学习系列(一)——Jps
  6. 秋风秋雨愁煞人:寒宵独坐心如捣
  7. 解决IE浏览器URL乱码的问题
  8. 人工智能ai应用高管指南_理解人工智能指南
  9. mac Android studio 使用 阿里 maven 报错
  10. 2021-2027全球与中国乳胶机械稳定性测试仪市场现状及未来发展趋势
  11. logback 配置总结
  12. babel-plugin-transform-remove-consol插件的安装及使用(作用是移除代码里的所有console.log())
  13. 求解TSP问题神器——elkai(简单好用)
  14. 【Linux】mysql命令行查看表结构,字段等信息
  15. catia二次开发:vb的 GetObject失败,解决办法
  16. c语言+结构体指针初始化,c语言结构体指针初始化===
  17. 《光纤通信系统》小结
  18. 联发科p60和骁龙710哪个好_骁龙710、麒麟710和联发科P60性能对比测试 哪个好?...
  19. 一文看懂云计算、雾计算、霾计算、边缘计算以及认知计算
  20. 关于 UGUI 字体花屏或乱码

热门文章

  1. WinCE的网络驱动实现原理
  2. 几种无线充电解决方案特点及原理图
  3. HTML页面加载异常,按F12调试后居然又好了的解决办法!
  4. 解决方案,org.hibernate.LazyInitializationException: could not initialize proxy - no Session
  5. spring使用@Value注解读取.properties文件时出现中文乱码问题的解决
  6. 【报告分享】2020年数字政府新基建发展白皮书.pdf(附下载链接)
  7. 2020,人工智能与产业结合的新纪年,你需要哪些能力可以脱颖而出?
  8. 知乎推荐算法工程师面经
  9. 推荐系统之协同过滤算法分布式实现(附代码实现)
  10. 推荐算法工程师必备!!!协同过滤推荐算法总结