题意:给定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(回文数字)的更多相关文章

  1. [LeetCode] Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  2. javascript 实现一个回文数字

    写一个方法,让"1234"变成回文数字“1234321”,就是顺着读和倒着读都是一样的:注:不让用reverse()方法: function palindrome(str){ va ...

  3. Java 简单算法--打印回文数字

    package cn.magicdu.algorithm; public class CircleNumber { public static void main(String[] args) { f ...

  4. [蓝桥杯]PREV-21.历届试题_回文数字

    问题描述 观察数字:, 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做:回文数字. 本题要求你找到一些5位或6位的十进制数字.满足如下要求: 该数字的各个数位之和等于输入 ...

  5. 【蓝桥杯】PREV-21 回文数字

    题目链接:http://lx.lanqiao.org/problem.page? gpid=T113   历届试题 回文数字   时间限制:1.0s   内存限制:256.0MB      问题描写叙 ...

  6. LeetCode 9 Palindrome Number(回文数字判断)

    Long Time No See !   题目链接https://leetcode.com/problems/palindrome-number/?tab=Description   首先确定该数字的 ...

  7. 算法笔记_181:历届试题 回文数字(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 观察数字:12321,123321 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做:回文数字. 本题要求你找 ...

  8. 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...

  9. Java小程序之回文数字

    题目:一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. 下面是代码: package test; public class BackNum { public st ...

随机推荐

  1. phpcms v9和discuz X3.1实现同步登陆退出论坛(已实现)

    网络上文章很多,按步骤配置好了之后phpcms可以同步登录dz,但是dz登录后状态却无法同步到phpcms,网络上找了很多资料都大同小异,头大.只能自己调试了,废话不多说了.       以下网络上抄 ...

  2. Android版本号的识别——$(PLATFORM_VERSION)

    #/******************************************************************************#*@file Android.mk#* ...

  3. ios开发——面试篇C语言精华

    面试篇C语言精华    1.面向过程:分析解决问题所需要的步骤,然后用函数把这些步骤一步一步实 现. 面向对象:直接描述客观世界的对象及其相互关系.现实世界中任何实体都 可以看作是对象,对象之间通过消 ...

  4. Solaris 安装JDK

    http://blog.csdn.net/cymm_liu/article/details/46966237  整理自前辈的博客:http://segmentfault.com/a/119000000 ...

  5. myloader原理0

    开源MySQL多线程逻辑导入工具myloader原理与改进 在上一篇中,介绍了多线程备份工具mydumper的实现及网易对其所做的优化,本篇聊聊与mydumper配合使用的myloader工具. my ...

  6. c高级编程4 LD_DEBUG

    [root@monitor ~]# LD_DEBUG=help ls Valid options for the LD_DEBUG environment variable are: libs dis ...

  7. Type mytableview does not confirm to portocol UITableViewDataResource

    继承UITableViewDataSource报上面这个总是,是重写协议时写错了 override func numberOfRowsInSection(section: Int) -> Int ...

  8. iOS如何随意的穿插跳跃,push来pop去

    iOS如何随意的穿插跳跃,push来pop去? 主题思想:如A.B.C.D 四个视图控制器. 想要在 A push B 后, B 在push 到 D ,然后从 D pop 到 C ,在从 C pop ...

  9. Magical Forest

    Problem Description There is a forest can be seen as N * M grid. In this forest, there is some magic ...

  10. ceph运维命令合集

    一.集群 1.启动一个ceph进程 启动mon进程 [root@ceph-adm ~]#203.109 service ceph start mon.ceph-mon1 启动msd进程 [root@c ...