LightOJ - 1396 :Palindromic Numbers (III)(逐位确定法)
Vinci is a little boy and is very creative. One day his teacher asked him to write all the Palindromic numbers from 1 to 1000. He became very frustrated because there is nothing creative in the task. Observing his expression, the teacher replied, "All right then, you want hard stuff, you got it." Then he asks Vinci to write a palindromic number which is greater than the given number. A number is called palindromic when its digits are same from both sides. For example: 1223221, 121, 232 are palindromic numbers but 122, 211, 332 are not. As there can be multiple solutions, Vinci has to find the number which is as small as possible.
Input
Input starts with an integer T (≤ 30), denoting the number of test cases.
Each case starts with a line containing a positive integer. This integer can be huge and can contain up to 105 digits.
Output
For each case, print the case number and the minimum possible palindromic number which is greater than the given number.
Sample Input
5
121
1
1332331
11
1121
Sample Output
Case 1: 131
Case 2: 2
Case 3: 1333331
Case 4: 22
Case 5: 1221
题意:输出比X大的第一个回文串。
思路:做过很多次了感觉,还是WA了几发。 按照如下几个步骤。
1,我们先按照左边几位对称到右边,如果比原串大,输出。
2,从中间到左边找第一个非‘9’的字符,+1,中间的全部变为‘0’。
3,全部都是‘9’,则长度+1,首尾为‘1’,其他为‘0’;
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
char d[maxn],a[maxn];int cnt;
void put(int len)
{
if(len==-){
putchar(''); rep(i,,cnt-) putchar('');
putchar(''); putchar('\n'); return ;
}
rep(i,,len) putchar(a[i]); putchar('\n');
}
void cal()
{
cnt=strlen(d+);
rep(i,,cnt/) swap(d[i],d[cnt+-i]);
rep(i,cnt/+,cnt) a[i]=d[i];
rep(i,,cnt/) a[i]=a[cnt+-i];
for(int i=cnt;i>=;i--)
if(a[i]>d[i]){
put(cnt); return ;
}
else if(a[i]<d[i]) break;
rep(i,cnt/+,cnt){
if(a[i]<'') {
a[i]++;
rep(j,cnt-i+,i-) a[j]='';
a[cnt+-i]=a[i];
put(cnt); return ;
}
}
put(-); return ;
}
int main()
{
int T,C=,x;
scanf("%d",&T);
while(T--){
scanf("%s",d+);
printf("Case %d: ",++C);
cal();
}
return ;
}
LightOJ - 1396 :Palindromic Numbers (III)(逐位确定法)的更多相关文章
- LightOJ 1205 Palindromic Numbers
数位DP.... Palindromic Numbers Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %l ...
- LightOJ - 1205:Palindromic Numbers (数位DP&回文串)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- xtu summer individual 1 E - Palindromic Numbers
E - Palindromic Numbers Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %l ...
- Lightoj1205——Palindromic Numbers(数位dp+回文数)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- BFS:Open and Lock(一个数的逐位变化问题的搜索)
解体心得: 1.关于定义四维数组的问题,在起初使用时,总是在运行时出错,找了很多方法,最后全部将BFS()部分函数写在主函数中,将四维数组定义在主函数中才解决了问题.运行成功后再次将四维数组定义为全局 ...
- [暑假集训--数位dp]LightOj1205 Palindromic Numbers
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- led跑马灯多种方法(移位法,位拼接法,调用模块法,位移及位拼接语法,testbench的理解,源文件的存储路径,计数器的个数,调用模块的方式)
跟着教程写了几种方法,才发现自己写的虽然能实现,但比较繁琐.教程有三种方法: 1.移位法,每次左移一位,相比我自己写的,优点是不用把每一种情况都写出来.但是需要考虑左移到最后一位时需要自己再写个赋值语 ...
- 【LightOJ - 1205】Palindromic Numbers
[链接]https://cn.vjudge.net/problem/LightOJ-1205 [题意] 求出L..R范围内的回文个数 [题解] 数位DP; 先求出1..x里面的回文串个数.则做一下前缀 ...
- Palindromic Numbers LightOJ - 1205
题目大意: 求区间内的回文数个数 题目思路: 数位dp,先枚举前一半数字,然后填上相应的后一半数字. #include<cstdio> #include<cstring> #i ...
随机推荐
- Android初体验之Monkey和MonkeyRunner
原文地址https://blog.csdn.net/mad1989/article/details/38087737 Monkey 什么是Monkey Monkey是Android中的一个命令行工具, ...
- MyBatis—mybatis-config.xml配置介绍
在定义sqlSessionFactory时需要指定MyBatis主配置文件: Xml代码 说明: 收藏代码 1. <bean id="sqlSessionFactory" ...
- js动态创建Form表单并提交
javascript动态创建Form表单和表单项,然后提交表单请求,最后删除表单,代码片段如下(Firefox测试通过): var dlform = document.createElement('f ...
- CentOS禁用笔记本touchpad
自己在家笔记本装来个双系统玩玩,发现触摸板很烦人,禁用! 1.安装一个小神器 yum install xorg-x11-apps 2.查看你到输入硬件对应的id,方便禁用命令 [root@huangz ...
- SpringMVC中@pathVariable和@RequestParam注解的区别
@pathVariable和@RequestParam的区别 @pathVariable:是从路径中获取变量,也就是把路径当做变量 @RequestParam:是从请求里面获取参数 案例分析: /Sp ...
- HDU1059 二进制拆分优化多重背包
/*问你能不能将给出的资源平分成两半,那么我们就以一半为背包,运行多重背包模版 但是注意了,由于个数过大,直接运行会超时,所以要用二进制拆分每种的个数*/ #include<stdio.h> ...
- linux及安全第六周总结——20135227黄晓妍
总结部分: 操作系统内核三大功能: 进程管理,内存管理,文件系统 最核心的是进程管理 为了管理,首先要对每一个进程进行描述.进程描述符提供了所有内核需要了解的信息. 进程控制模块:task_struc ...
- 20145311 《Java程序设计》第十周学习总结
20145311 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 ·网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据·程序员所作的事情就是把数据发送到指定的位置 ...
- [问题解决]win10误删启动项(BCD)(HP电脑亲测,无需启动盘,并非重装系统)
昨天使用easyBCD软件,开始不太懂,手残把win10的引导删除了,后来发现电脑关机总是变成重启,无奈强制关机.今天重启了一下电脑,发现电脑已经无法打开了,这才明白昨天是误删了win10的BCD. ...
- Cuda 9.2 CuDnn7.0 官方文档解读
目录 Cuda 9.2 CuDnn7.0 官方文档解读 准备工作(下载) 显卡驱动重装 CUDA安装 系统要求 处理之前安装的cuda文件 下载的deb安装过程 下载的runfile的安装过程 安装完 ...