1.题目

  如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式。

2.样例

1  --> 2

9  -->11

12345 -->12421

123456 -->124421

999 -->1001

3.分析

  借用:http://www.cnblogs.com/xudong-bupt/p/4015226.html

4.代码

  1. import java.util.Scanner;
  2.  
  3. public class SymmetricNumber {
  4.  
  5. public static void main(String[] argv){
  6.  
  7. Scanner in=new Scanner(System.in);
  8. int N=in.nextInt();
  9. String n=String.valueOf(N);
  10.  
  11. //特殊情况:9999999999999.........
  12. if((N+1)%10==0)
  13. System.out.print(N+2);
  14.  
  15. //非特殊情况
  16. else
  17. {
  18.  
  19. if(n.length()==1){
  20. System.out.println(N+1);
  21. }
  22. else{
  23.  
  24. //偶数位
  25. if(n.length()%2==0){
  26. String temp=n.substring(0,n.length()/2);
  27. String temp_0=n.substring(n.length()/2,n.length());
  28. String temp_1="";
  29. for(int i=n.length()/2-1;i>=0;i--){
  30. temp_1=temp_1+temp.charAt(i);
  31. }
  32.  
  33. //大于的话则直接输出前半部分和前半部分的倒置
  34. if(Integer.parseInt(temp_1)>Integer.parseInt(temp_0))
  35. System.out.println(temp+temp_1);
  36.  
  37. //否则前半部分加一,然后新的temp与temp的倒置组成新的String 输出
  38. else
  39. {
  40. temp=String.valueOf(Integer.parseInt(temp)+1); //加一
  41. //本身加倒置组成新的String
  42. for(int i=temp.length()-1;i>=0;i--){
  43. temp=temp+temp.charAt(i);
  44. }
  45. System.out.println(temp);
  46. }
  47.  
  48. }
  49.  
  50. //奇数位
  51. else{
  52. String temp_0=n.substring((n.length()+1)/2,n.length());
  53. String temp=n.substring(0,(n.length()+1)/2);
  54. String temp_1="";
  55. for(int i=temp.length()-2;i>=0;i--){
  56. temp_1=temp_1+temp.charAt(i);
  57. }
  58. if(Integer.parseInt(temp_1)>Integer.parseInt(temp_0))
  59. System.out.println(temp+temp_1);
  60. else
  61. {
  62. temp=String.valueOf(Integer.parseInt(temp)+1);
  63. for(int i=temp.length()-2;i>=0;i--){
  64. temp=temp+temp.charAt(i);
  65. }
  66. System.out.println(temp);
  67. }
  68. }
  69.  
  70. }
  71.  
  72. }
  73.  
  74. }
  75. }

大于非负整数N的第一个回文数 Symmetric Number的更多相关文章

  1. [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number

    1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 这个题目也是当时笔试第一次见到,花了一个小时才做出了.慢慢总结还是挺简单的. 2.分析 分析如下: (1)一位数N(9 ...

  2. [Swift]LeetCode9. 回文数 | Palindrome Number

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  3. Leetcode 9 回文数Palindrome Number

    判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...

  4. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  5. 判断回文字符串、回文链表、回文数(python实现)

    所谓回文字符串,就是正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.即是对称结构 判断回文字符串 方法一: def is_palin ...

  6. LeetCode(9):回文数

    Easy! 题目描述: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: f ...

  7. LeetCode 5回文数

    判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...

  8. Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解

    Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全   Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...

  9. C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数

    各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 输出: 解释: 各位相加的过程为: + = , + = . 由于 是一位数,所以返回 . 进阶:你可以 ...

随机推荐

  1. printk一些技巧【转】

    转自:http://haohetao.iteye.com/blog/1147791 转自:http://blog.csdn.net/wbd880419/article/details/73530550 ...

  2. [c++,bson] linux 使用 BSON 编程[www]

    [c++,bson] linux 使用 BSON 编程 http://blog.chinaunix.net/uid-28595538-id-4987410.html 1.js db=db.getSib ...

  3. bzoj 1179 Atm

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1179 题解: 一道比较综合的图论题 直接讲正解: 如果这个图G中存在某个强连通分量,那么这 ...

  4. mysql utf8改utf8mb4

    由于需要用到utf8mb4,之前是utf8现在给改成utf8mb4 查看当前环境 SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' ...

  5. Cent os FTP配置

    原文:http://www.aicoffees.com/itshare/412261137.html

  6. 几条学习python的建议

    熟悉python语言, 以及学会python的编码方式. 熟悉python库, 遇到开发任务的时候知道如何去找对应的模块. 知道如何查找和获取第三方的python库, 以应付开发任务. 学习步骤 安装 ...

  7. MiCode108 猜数字

    Description 相传,十八世纪的数学家喜欢玩一种猜数字的小游戏,规则如下: 首先裁判选定一个正整数数字 N (2 \leq N \leq 200)N(2≤N≤200),然后选择两个不同的整数X ...

  8. 爬虫基础库之requests

    requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...

  9. matlab安装及使用

    matlab R2015b在ubuntu 14.04环境下的安装 挂载及运行安装程序 sudo mkidr /media/matlab mount -o loop matlab_R2015b.iso ...

  10. 树莓派使用opencv

    安装 reference1 reference2 注意 安装顺利,但是使用的时候提示 you need install libgtk2.0-dev xxx ,这时候说明你安装的库的顺序不对,你应该先安 ...