博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Palindromic Numbers (III)(回文数,较麻烦)
阅读量:6788 次
发布时间:2019-06-26

本文共 3903 字,大约阅读时间需要 13 分钟。

Palindromic Numbers (III)
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
     

Description

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

Hint

Dataset is huge, use faster I/O methods.

AC CODE:

//Memory: 1608 KB		Time: 82 MS//Language: C++		Result: Accepted#include 
#include
#include
using namespace std;char s[100001];int main(){ int T, c = 1; int i, j, len, mid; bool odd, flag; scanf("%d", &T); while(T--) { scanf("%s", s); printf("Case %d: ", c++); //特判所有digit为9的情况 for(i = 0; s[i] == '9'; i++){} if(s[i] == '\0') { putchar('1'); for(i = 0; s[i+1] != '\0'; i++) putchar('0'); puts("1"); continue; } len = strlen(s); //特判只有一位数的情况 if(len < 2) { s[0]++; puts(s); continue; } mid = len / 2; odd = len & 1; if(odd) { for(i = mid - 1, j = mid + 1; i > -1 && s[i] == s[j]; i--, j++){} //原数是回文数 if(i == -1) { for(i = mid; s[i] == '9'; i++) s[len - i - 1] = s[i] = '0'; s[len - i - 1] = ++s[i]; puts(s); } else if(s[i] > s[j]) { for(i = 0; i < mid; i++) putchar(s[i]); for(; i > -1; i--) putchar(s[i]); puts(""); } //s[i] < s[j] else { /*for loop中的判断条件不需要i > -1,因为必定能找到s[i] != '9'的i。试想如果 s[i] == '9'一直成立,就不会有s[i] < s[j]了。*/ for(i = mid; s[i] == '9'; i--) s[i] = '0'; s[i]++; for(i = 0; i < mid; i++) putchar(s[i]); for(; i > -1; i--) putchar(s[i]); puts(""); } } //odd = false else { for(i = mid - 1, j = mid; i > -1 && s[i] == s[j]; i--, j++){} //原数是回文数 if(i == -1) { /*此处for loop不需要i
s[j]) { for(i = 0; i < mid; i++) putchar(s[i]); for(i = mid - 1; i > -1; i--) putchar(s[i]); puts(""); } //s[i] < s[j] else { //必定存在i使得s[i]!='9',不然不会有s[i] < s[j](这是进入这个else的条件) for(i = mid - 1; s[i] == '9'; i--) s[i] = '0'; s[i]++; for(i = 0; i < mid; i++) putchar(s[i]); for(i = mid - 1; i > -1; i--) putchar(s[i]); puts(""); } } }}

转载地址:http://fmigo.baihongyu.com/

你可能感兴趣的文章
HDU-Fish买电脑 二分查找
查看>>
Rzagovori 贪心
查看>>
java日期格式(年月日时分秒毫秒)
查看>>
linux nohup后台运行命令
查看>>
[SDOI2017]天才黑客
查看>>
hbase 学习(十六)系统架构图
查看>>
sqlserver数据存储
查看>>
进行app性能和安全性测试的重要性
查看>>
Oracle学习总结(9)—— Oracle 常用的基本操作
查看>>
WebService学习总结(6)——WebService常用接口
查看>>
Mysql学习总结(36)——Mysql查询优化
查看>>
网页设计易错点总结
查看>>
assign-cookies
查看>>
<正则吃饺子> :关于redis配置文件参数详解
查看>>
2018-2019-2 20165334-Exp6:信息收集与漏洞扫描
查看>>
python发邮件模板参考
查看>>
【转载】rpc.rstatd安装与配置
查看>>
bootstrap课程10 从外部引入视频到页面用什么标签
查看>>
m_Orchestrate learning system---二十一、怎样写算法比较轻松
查看>>
LIUNX-Centos 7 编译GDAL
查看>>