A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

Input

The first line contains integer t, the number of test cases. Integers K are given in the next t lines.

Output

For each K, output the smallest palindrome larger than K.

Example

  1. Input:
  2. 2
  3. 808
  4. 2133
  5.  
  6. Output:
  7. 818
  8. 2222

Warning: large Input/Output data, be careful with certain languages

题意:输出比X大的第一个回文字符串。

思路:先把X按左半边为标准变成一个回文串X2,如果X2大于X,则输出X2。 否则变大X2 :

如果X2全部为9,则需要加一位,变为首尾为‘1’,之间为‘0’的回文串。

否则,从之间开始找第一位非‘9’的位置,自加1。然后中间取余变为‘0’。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<iostream>
  5. #include<algorithm>
  6. using namespace std;
  7. const int maxn=;
  8. char c[maxn],c2[maxn];
  9. int T,N,Len,a[maxn];
  10. bool check9()
  11. {
  12. for(int i=;i<=Len;i++)
  13. if(c[i]!='') return false;
  14. return true;
  15. }
  16. void Tochange()
  17. {
  18. for(int i=;i<=Len/;i++) c2[i]=c[i];
  19. for(int i=Len/+;i<=Len;i++) c2[i]=c[Len+-i];
  20. }
  21. bool Toupper()
  22. {
  23. for(int i=;i<=Len;i++)
  24. if(c2[i]>c[i]) return true;
  25. else if(c2[i]<c[i]) return false;
  26. return false;
  27. }
  28. int main()
  29. {
  30. int i,j;
  31. scanf("%d",&T);
  32. while(T--){
  33. scanf("%s",c+);
  34. Len=strlen(c+);
  35. Tochange();
  36. if(Toupper()) {
  37. for(i=;i<=Len;i++) putchar(c2[i]);
  38. cout<<endl;
  39. continue;
  40. }
  41. if(check9()) {
  42. putchar('');
  43. for(i=;i<Len;i++) putchar('');
  44. putchar('');
  45. cout<<endl;
  46. continue;
  47. }
  48. int np,Mid;
  49. if(Len&) Mid=(Len+)/;
  50. else Mid=Len/;
  51. for(np=Mid;np>=;np--) if(c[np]!='') break;
  52. c[np]++;
  53. for(i=np+;i<=Mid;i++) c[i]='';
  54. for(i=;i<=Mid;i++) putchar(c[i]);
  55. for(i=Len/;i>=;i--) putchar(c[i]);
  56. cout<<endl;
  57. }
  58. return ;
  59. }

SPOJ:The Next Palindrome(贪心&思维)的更多相关文章

  1. Mike and distribution CodeForces - 798D (贪心+思维)

    题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...

  2. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  3. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  4. 贪心/思维题 UVA 11292 The Dragon of Loowater

    题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...

  5. T - Posterized(贪心思维)

    Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...

  6. C. Coffee Break 贪心 思维 有点难 有意思

    C. Coffee Break 这个贪心之前好像写过,还是感觉挺难的,有点不会写. 这个题目大意是:给你一个数列n个元素,然后给你一天的时间,给你一个间隔时间d, 问你最少要用多少天可以把这个数列的所 ...

  7. Educational Codeforces Round 2 C. Make Palindrome 贪心

    C. Make Palindrome Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  8. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

  9. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

随机推荐

  1. zoj 2947 Abbreviation

    Abbreviation Time Limit: 2 Seconds      Memory Limit: 65536 KB When a Little White meets another Lit ...

  2. CodeForces 20 A+B

                                               A - BerOS file system 水题不解释了,压缩斜杆.要注意最后没有斜杠. char a[105]; ...

  3. Codeforces Round #377 (Div. 2)部分题解A+B+C!

    A. Buy a Shovel 题意是很好懂的,一件商品单价为k,但他身上只有10块的若干和一张r块的:求最少买几件使得不需要找零.只需枚举数量判断总价最后一位是否为0或r即可. #include&l ...

  4. 认识Linux文件系统的架构

    本文主要研究一下storm的OpaquePartitionedTridentSpoutExecutor TridentTopology.newStream storm-core-1.2.2-sourc ...

  5. [bzoj1787][Ahoi2008]Meet 紧急集合(lca)

    传送门 可以看出,三个点两两之间的lca会有一对相同,而另一个lca就是聚集点. 然后搞搞就可以求出距离了. ——代码 #include <cstdio> #include <cst ...

  6. JS中的call()和apply()方法区别

    如 果没接触过动态语言,以编译型语言的思维方式去理解javaScript将会有种神奇而怪异的感觉,因为意识上往往不可能的事偏偏就发生了,甚至觉得不可 理喻.如果在学JavaScript这自由而变幻无穷 ...

  7. Python基础教程笔记——第1章

    1.8 函数 pow(x,y) x^y abs(x)          取数的绝对值 round(x)   会把浮点数四舍五入为最接近的整数 floor(x)     向下取整的函数,但是需要先imp ...

  8. 给Ubuntu更换成163的源(sources.list)Unable to locate package

    Refer to http://www.crifan.com/ubuntu_change_sources_list_to_163/ 1. backup /etc/apt/sources.list 2. ...

  9. mysql针对转义字符的模糊搜索

    由于urlencode之后会产生很多'%'符号,这个符号在mysql模糊搜索中代表任意字符,显示会出现问题,例如 name字段经过urlencode之后变成‘%E6%9D%8E%E5%87%A1’,如 ...

  10. 混合APP开发框架资料汇总

    Ionic(ionicframework)一款接近原生的Html5移动App开发框架 会html css js就可以开发app,Ionic基于angualrjs框架是一个专注于开发移动wap以及app ...