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 ...
随机推荐
- js判断用户是在PC端或移动端访问
js如何判断用户是在PC端和还是移动端访问. 最近一直在忙我们团队的项目“咖啡之翼”,在这个项目中,我们为移动平台提供了一个优秀的体验.伴随Android平台的红火发展.不仅带动国内智能手机行业,而 ...
- 凌华AMP-204C卡的CNC功能 即Feeder工具的使用。
Feeder 可以实现一些CNC之类的功能. 不过目前似乎只有两个轴的联动
- python开发之路目录
Python 目录 基础 python入门 python数据类型.字符编码.文件处理 python函数基础 python函数进阶 python装饰器函数 python装饰器函数 python递归函数 ...
- devise 自定义手机号登录
user model中配置 validates_uniqueness_of :phone def email_required? false end 修改user migration文件,给phone ...
- Codeforces Round #533 (Div. 2) Solution
A. Salem and Sticks 签. #include <bits/stdc++.h> using namespace std; #define N 1010 int n, a[N ...
- Python笔记 #12# Dictionary & Pandas: Object Creation
Document of Dictionaries 10 Minutes to pandas tutorialspoint import pandas as pd data = [['Alex',10] ...
- Python3.x:requests的用法
Python3.x:requests的用法 1,requests 比 urllib.request 容错能力更强: 2,通常用法: (1).认证.状态码.header.编码.json r = requ ...
- 四种常见的 POST 提交数据方式 专题
定义和用法 enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码.默认地,表单数据会编码为 "application/x-www-form-urlencoded". ...
- 什么是TensorFlow Serving
答:1. 从Serving 可以看出,与服务有关; 2. 那么为啥还有TensorFlow的前缀?肯定与TensorFlow有着很大的关系: 3. 那么Tensorflow是用来干什么的呢?Tenso ...
- Knockout 监控数组对象属性
代码: function Product(ProductID,ProductName,ProductNum,Result,Price) { this.ProductID = ko.observable ...