UVALive 2889(回文数字)
题意:给定i,输出第i个回文数字。
分析:1,2,3,4,……,9------------------------------------------------------------------------------------------9个
11,12,13,14,……,19-------------------------------------------------------------------------------------9个
101,111,121,131,141,151,161,171,181,191,202,212,222,232,……,979,989,999-------------------------------90个
1001,1111,1221,1331,1441,……,9889,9999----------------------------------------------------------------90个
10001,10101,10201,10301,……,99899,99999--------------------------------------------------------------900个
规律是把回文串一分为二看,例如第四行,前两个数字是从10到99,共90个数字。而第三行也是从10到99,区别在于,需要去掉最后一位再反转,才是另一半(后两个数字)。
思路:先确定i所对应的回文数字的位数,再确定具体值。
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<list>
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
#define pr(x) cout << #x << " : " << x << " "
#define prln(x) cout << #x << " : " << x << endl
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const double pi = acos(-1.0);
const double EPS = 1e-;
const int dx[] = {, , -, };
const int dy[] = {-, , , };
const ll MOD = 1e9 + ;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
ll a[];
void init()
{
ll tmp = ll();
for(int i = ; i < ; i += )
{
a[i] = a[i - ] = tmp;
tmp *= ll();
}
}
ll POW(ll x)
{
ll w = ;
for(ll i = ; i <= x; ++i)
w *= ll();
return w;
}
int main()
{
init();
int n;
while(scanf("%d", &n) == && n)
{
int cnt = ;
while(n > a[cnt])
{
n -= a[cnt];
++cnt;
}
if(cnt == )
{
printf("%d\n", n);
continue;
}
else if(cnt == )
{
printf("%d%d\n", n, n);
continue;
}
else
{
int tmp = cnt / ;
char str[];
memset(str, , sizeof str);
ll ans = POW(ll(cnt / )) + ll(n - );
sprintf(str, "%lld", ans);//把数字变为指定格式的字符串
printf("%s", str);
string s = string(str);
int len = s.size();
if(cnt % == ) s.resize(len - );
reverse(s.begin(), s.end());
printf("%s\n", s.c_str());
}
}
return ;
}
UVALive 2889(回文数字)的更多相关文章
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- javascript 实现一个回文数字
写一个方法,让"1234"变成回文数字“1234321”,就是顺着读和倒着读都是一样的:注:不让用reverse()方法: function palindrome(str){ va ...
- Java 简单算法--打印回文数字
package cn.magicdu.algorithm; public class CircleNumber { public static void main(String[] args) { f ...
- [蓝桥杯]PREV-21.历届试题_回文数字
问题描述 观察数字:, 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做:回文数字. 本题要求你找到一些5位或6位的十进制数字.满足如下要求: 该数字的各个数位之和等于输入 ...
- 【蓝桥杯】PREV-21 回文数字
题目链接:http://lx.lanqiao.org/problem.page? gpid=T113 历届试题 回文数字 时间限制:1.0s 内存限制:256.0MB 问题描写叙 ...
- LeetCode 9 Palindrome Number(回文数字判断)
Long Time No See ! 题目链接https://leetcode.com/problems/palindrome-number/?tab=Description 首先确定该数字的 ...
- 算法笔记_181:历届试题 回文数字(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 观察数字:12321,123321 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做:回文数字. 本题要求你找 ...
- 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...
- Java小程序之回文数字
题目:一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. 下面是代码: package test; public class BackNum { public st ...
随机推荐
- HDU 4343 D - Interval query 二分贪心
D - Interval queryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...
- javaio学习笔记-字符流类(1)
1.java.io包中的字符流类-BufferedReader和BufferedWriter: BufferedReader:缓存的输入字符流; BufferedWriter:缓存的输出字符流; In ...
- [AngularJS] New in Angular 1.3 - Performance Boost with debugInfoEnabled
By default, Angular provides a lot of debug information on the DOM that's only necessary for tools l ...
- iOS开发——网络编程Swift篇&(四)异步Get方式
异步Get方式 // MARK: - 异步Get方式 func asynchronousGet() { //创建NSURL对象 var url:NSURL! = NSURL(string: " ...
- 检测到有潜在危险的 Request.Form
今天在做一个.net的新闻发布器的时候. 遇到这样的一个问,在html编辑器里面加入图片提交的时候 就报一个 从客户端(content1="<img src="/web/ne ...
- Solaris 安装JDK
http://blog.csdn.net/cymm_liu/article/details/46966237 整理自前辈的博客:http://segmentfault.com/a/119000000 ...
- 日志管理 rsyslog服务浅析
http://www.xiaomastack.com/2014/11/13/rsyslog/
- 网口扫盲三:以太网芯片MAC和PHY的关系
转载:http://www.cnblogs.com/jason-lu/articles/3195473.html 问:如何实现单片以太网微控制器? 答:诀窍是将微控制器.以太网媒体接入控制器(MA ...
- 使用apache和IIS,共用80端口的一个解决方案【转】
将apache设为使用80端口,IIS使用其它端口,比如81,然后将apache作为IIS的代理. 在httpd.conf里面,取消下面四行的注释: LoadModule proxy_module m ...
- 转 oracle 11g 导出空表
1.Oracle11g默认对空表不分配segment,故使用exp导出Oracle11g数据库时,空表不会导出. 2.设置deferred_segment_creation 参数为FALSE后,无论是 ...