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个回文数为止(也许大部分人一开始都是 ...
随机推荐
- 洛谷 2822 组合数问题——质因数有关的dp
题目:https://www.luogu.org/problemnew/show/P2822 发现 k 都是一样的.所以可以设dp[ i ][ j ]表示 n<=i,m<=j 的答案.发现 ...
- mac下安装node
学着使用homebrew进行安装,发现很是方便. homebrew是mac下的一款管理安装的工具. 1. 安装homebrew 使用mac自带的ruby下载安装: ruby -e "$(cu ...
- GSM/GPRS操作示例
A6(GPRS) 博文转自安信科技: http://wiki.ai-thinker.com/gprs/examples 1.电话接收 相关指令 ATDxxxxx;//xxx为电话号码 示例 打电话 ...
- C# 利用委托和事件 传入一个参数进行进行计算并返回结果
一.委托定义 1: public class TestData 2: { 3: //定义委托 4: public delegate void Get_TestDataEventHandler(Get_ ...
- 问题:c# newtonsoft.json使用;结果:Newtonsoft.Json 用法
Newtonsoft.Json 用法 Newtonsoft.Json 是.NET 下开源的json格式序列号和反序列化的类库.官方网站: http://json.codeplex.com/ 使用方法 ...
- GIF助手帮助说明
生成的文件位置? 生成的GIF等文件存储在 /sdcard/gifdir/ 目录下. 为什么分享到微信.Twitter等平台失败? 微信.Twitter等平台对GIF文件大小有限制,微信限制为1MB, ...
- Mybaits整合Spring自动扫描 接口,Mybaits配置文件.xml文件和Dao实体类
1.转自:https://blog.csdn.net/u013802160/article/details/51815077 <?xml version="1.0" enco ...
- PopupWindow --- 弹出底部窗体
第一步 : 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- Saving output of a grep into a file with colors
19 down vote favorite 7 I need to save the result of a grep command into a file, but I also want the ...
- Mathematics Base - 期望、方差、协方差、相关系数总结
参考:<深度学习500问> 期望 在概率论和统计学中,数学期望(或均值,亦简称期望)是试验中每次可能结果的概率乘以其结果的总和.它反映随机变量平均取值的大小. 线性运算: \(E(ax+ ...