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 ...
随机推荐
- 通过程序 VB.Net 或 C# 读取文本文件行数
1, VB.NET 读取 (通过streamReader) ' tmpCount = 0 'Dim tmpSR As New StreamReader(fileFullName, System.Tex ...
- hdu4864 hdu4268 贪心 lower_bound
hdu4864 题意: 有n个机器,m个任务,n,m<=100000,每个机器都有工作时间的最大限制xi(0<xi<1440)和完成的最大难度yi(0<=yi<=100) ...
- android Camera 数据流程分析
这篇文章主要针对其数据流程进行分析.Camera一般用于图像浏览.拍照和视频录制.这里先对图像浏览和拍照的数据流进行分析,后面再对视频电话部分进行分析. 1.针对HAL层对摄像头数据处理补充一下 Li ...
- java中接口的定义与实现
1.定义接口 使用interface来定义一个接口.接口定义同类的定义类似,也是分为接口的声明和接口体,当中接口体由常量定义和方法定义两部分组成.定义接口的基本格式例如以下: [修饰符] in ...
- 一元线性回归模型与最小二乘法及其C++实现
原文:http://blog.csdn.net/qll125596718/article/details/8248249 监督学习中,如果预测的变量是离散的,我们称其为分类(如决策树,支持向量机等), ...
- C#_ajax fileupload
@{ ViewBag.Title = "Index"; Layout = null; } <h2>Index</h2> <script src=&qu ...
- [转] 详解http和https的作用与区别
PS: https就是http和TCP之间有一层SSL层,这一层的实际作用是防止钓鱼和加密.防止钓鱼通过网站的证书,网站必须有CA证书,证书类似于一个解密的签名.另外是加密,加密需要一个密钥交换算法, ...
- 还在用GCD?来看看NSOperation吧
在iOS开发中,谈到多线程,大家第一时间想到的一定是GCD.GCD固然是一套强大的多线程解决方案,能够解决绝大多数的多线程问题,但是他易于上手难于精通且到处是坑的特点也注定了想熟练使用它有一定的难度. ...
- C语言有关数组的几点
动态大小数组 先来看一段代码: ; int a[n]; int i; ;i<n;i++) { a[i]=i; printf("%d\n",a[i]); } 我在声明数组a时, ...
- WIN2003服务器IIS下如何开启GZIP压缩
在上一篇文章黑客流谈到了关于网页打开速度对SEO的影响,其中提到了网页开启Gzip压缩的好处,接下来我来和大家分享一下WINDOWS系统IIS服务器下如何开启Gzip压缩. 首先我们来了解一下什么是G ...