E - Palindrome Numbers
题目链接: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的更多相关文章
- POJ2402/UVA 12050 Palindrome Numbers 数学思维
A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...
- Palindrome Numbers(LA2889)第n个回文数是?
J - Palindrome Numbers Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- Uva - 12050 Palindrome Numbers【数论】
题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...
- UVa 12050 - Palindrome Numbers (回文数)
A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example, th ...
- Leetcode: Palindrome Numbers
Determine whether an integer is a palindrome. Do this without extra space. 尝试用两头分别比较的方法,结果发现无法解决1000 ...
- POJ 2402 Palindrome Numbers
题目链接 水题,LA我居然没找到在那里. #include <cstdio> #include <cstring> #include <string> #inclu ...
- UVA 12050 - Palindrome Numbers 模拟
题目大意:给出i,输出第i个镜像数,不能有前导0. 题解:从外层开始模拟 #include <stdio.h> int p(int x) { int sum, i; ;i<=x;i+ ...
- LA 2889 (找规律) Palindrome Numbers
输出第n个回文数. 规律就是一位和两位的回文数各有9个,三位四位的回文数各有90个,以此类推. 给出n,可以先判定一下第n个回文数的位数,然后后面也不难推,但是有很多细节需要注意. #include ...
- POJ2402 Palindrome Numbers 回文数
题目链接: http://poj.org/problem?id=2402 题目大意就是让你找到第n个回文数是什么. 第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是 ...
随机推荐
- HDOJ1059(多重背包)
1.解法一:多重背包 #include<iostream> #include<cstdio> using namespace std; #define MAX(a,b) (a& ...
- 【转】 Pro Android学习笔记(四四):Dialog(1):触发Dialog
目录(?)[-] 创建dialog fragment Activity显示对话框 Android提供alert.prompt.pick-list,单选.多选,progress.time-picker和 ...
- 发一个可伸缩线程池大小的python线程池。已通过测试。
发一个可伸缩线程池大小的线程池. 当任务不多时候,不开那么多线程,当任务多的时候开更多线程.当长时间没任务时候,将线程数量减小到一定数量. java的Threadpoolexcutor可以这样,py的 ...
- java中的equals方法
这个方法首先比较的是两个对象的地址是否相同,如果相同直接返回true, 否则, (1)如果是string类型的先比较是否是string类型,是的话,再比较是否长度相同,相同的话再比较,每个字符是否相同 ...
- Shrio00 Shiro角色授权、Shiro权限授权、开启Shiro缓存
1 需求01 用户进行过认证登录后,某些接口是有权限限制的:如何实现只有相应权限的用户才可以调用相应接口 2 修改shiro配置类 ShiroConfiguration package cn.xia ...
- SpringMVC RESTful中文乱码
开发中常遇到各种中文乱码很少心烦,这里总结了各种中文乱码https://www.cnblogs.com/lwx521/p/9856186.html 下面以SpringMVC遇到的中文乱码为例详解 首先 ...
- hdu 1576 A/B (求逆元)
题目链接 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Inpu ...
- .NET 中文件嵌套,例如:cshtml文件下面嵌套css和js【机器翻译】
越来越多的我发现自己在我的一些较大的Web项目中丢失了文件.有很多内容要处理 - HTML视图,几个派生CSS页面,页面级CSS,脚本库,应用程序范围的脚本和页面特定的脚本文件等等.幸运的是,我使用R ...
- SQL Server事务回滚对自增键的影响
SQL Server事务回滚时是删除原先插入导致的自增值,也就是回滚之前你你插入一条数据导致自增键加1,回滚之后还是加1的状态 --如果获取当前操作最后插入的identity列的值:select @@ ...
- Java面向对象的三大特性 继承
继承是类与类的一种关系,是“is a"关系 子类拥有父类的属性和方法,private除外 class 子类 extends 父类 方法的重写 调用方法时会优先调用子类的方法 重写时,返 ...