UVa 12050 - Palindrome Numbers (回文数)
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个回文串
思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然后就得到是长度为多少的第几个的回文串了,有个细节注意的是,
n计算完后要-1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;
const int maxn = ;
ll num[maxn];
int n, ans[maxn];
void init() {
num[] = , num[] = num[] = ;
for (int i = ; i < ; i += )
num[i] = num[i+] = num[i-] * ;
}
int main() {
init();
while (scanf("%d", &n) && n) {
int len = ;
while (n > num[len]) {
n -= num[len];
len++;
}
n--;
int cnt = len / + ;
while (n) {
ans[cnt++] = n % ;
n /= ;
}
for (int i = cnt; i <= len; i++)
ans[i] = ;
ans[len]++;
for (int i = ; i <= len/; i++)
ans[i] = ans[len-i+];
for (int i = ; i <= len; i++)
printf("%d", ans[i]);
printf("\n");
}
return ;
}
UVa 12050 - Palindrome Numbers (回文数)的更多相关文章
- POJ2402 Palindrome Numbers 回文数
题目链接: http://poj.org/problem?id=2402 题目大意就是让你找到第n个回文数是什么. 第一个思路当然是一个一个地构造回文数直到找到第n个回文数为止(也许大部分人一开始都是 ...
- Uva - 12050 Palindrome Numbers【数论】
题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...
- POJ2402/UVA 12050 Palindrome Numbers 数学思维
A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- Leetcode 3——Palindrome Number(回文数)
Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- [LeetCode]9. Palindrome Number回文数
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
随机推荐
- 常用MySQL操作
常用MySQL操作 更改MySQL数据库root的密码 将绝对路径加入环境变量并设置开机启动 # PATH=$PATH:/usr/local/mysql/bin # echo "PATH=$ ...
- 微信小程序入门笔记-小程序创建(2)
1.工具下载 官方链接:https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 我选用的是稳定版 macOS 2 ...
- [20200223]关于latch and mutext的优化.txt
[20200223]关于latch and mutext的优化.txt --//前一段时间一直在测试使用DBMS_SHARED_POOL.MARKHOT标识热对象以及sql语句的优化.--//有别人问 ...
- Python之write与writelines区别
一.传入的参数类型要求不同: 1. file.write(str)需要传入一个字符串做为参数,否则会报错. write( "字符串") with open('20200222.tx ...
- AI Web 1.0
kali:192.168.0.103 目标机:192.168.0.105 0X01 端口和目录扫描 打开目标主页没有任何信息 a) 端口扫描 只有一个80端口开启 b) 目录扫描 0x03 查看敏感目 ...
- echarts--可拖动的曲线图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【37】池化层讲解(Pooling layers)
池化层(Pooling layers) 除了卷积层,卷积网络也经常使用池化层来缩减模型的大小,提高计算速度,同时提高所提取特征的鲁棒性,我们来看一下. 先举一个池化层的例子,然后我们再讨论池化层的 ...
- 解决kbmMW Scheduler在任务中停止任务遇到的问题
procedure TCustomGridViewCameraDoc.InitSchedule; begin Scheduler.Schedule( procedure(const ASchedule ...
- ABP前端-关于不同按钮调用同一事件传入的参数变为相同的数据
现象: 在一个含有的Tab标签的页面,两个标签页的新增按钮调用同一个新增事件并传入不同的参数,但实际在调用的的时候传入的参数都变成了最后一个按钮传入的值,即,不论点击哪个Tab按钮的新增事件,最终传入 ...
- webdriver中的定位\模拟\及实例
Selenium webdriver 下载 pip install selenium pip install -i https://pypi.doubanio.com/simple/ selenium ...