zoukankan      html  css  js  c++  java
  • 大于非负整数N的第一个回文数 Symmetric Number

    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 
     4 public class SymmetricNumber {
     5     
     6     
     7     public static void main(String[] argv){
     8         
     9         Scanner in=new Scanner(System.in);
    10         int N=in.nextInt();
    11         String n=String.valueOf(N);
    12         
    13         //特殊情况:9999999999999.........
    14         if((N+1)%10==0)
    15             System.out.print(N+2);
    16         
    17         //非特殊情况
    18         else    
    19         {
    20             
    21             if(n.length()==1){
    22                 System.out.println(N+1);
    23             }
    24             else{
    25                 
    26                 //偶数位
    27                 if(n.length()%2==0){
    28                 String temp=n.substring(0,n.length()/2);
    29                 String temp_0=n.substring(n.length()/2,n.length());
    30                 String temp_1="";
    31                 for(int i=n.length()/2-1;i>=0;i--){                
    32                     temp_1=temp_1+temp.charAt(i);
    33                 }
    34                 
    35                 //大于的话则直接输出前半部分和前半部分的倒置
    36                 if(Integer.parseInt(temp_1)>Integer.parseInt(temp_0))
    37                     System.out.println(temp+temp_1);
    38                 
    39                 //否则前半部分加一,然后新的temp与temp的倒置组成新的String 输出
    40                 else
    41                 {
    42                     temp=String.valueOf(Integer.parseInt(temp)+1); //加一
    43                     //本身加倒置组成新的String
    44                     for(int i=temp.length()-1;i>=0;i--){                
    45                         temp=temp+temp.charAt(i);
    46                     }
    47                     System.out.println(temp);
    48                 }
    49             
    50                 }
    51             
    52                 //奇数位
    53                 else{
    54                     String temp_0=n.substring((n.length()+1)/2,n.length());
    55                     String temp=n.substring(0,(n.length()+1)/2);
    56                     String temp_1="";
    57                     for(int i=temp.length()-2;i>=0;i--){                
    58                         temp_1=temp_1+temp.charAt(i);                    
    59                     }                    
    60                     if(Integer.parseInt(temp_1)>Integer.parseInt(temp_0))
    61                         System.out.println(temp+temp_1);
    62                     else
    63                     {
    64                         temp=String.valueOf(Integer.parseInt(temp)+1);    
    65                         for(int i=temp.length()-2;i>=0;i--){                
    66                             temp=temp+temp.charAt(i);
    67                         }
    68                         System.out.println(temp);
    69                     }
    70                 }
    71         
    72             }
    73         
    74         }
    75         
    76     }
    77 }
  • 相关阅读:
    GIT(分布式版本控制系统)
    mysql入门
    百度云上传下载会出现乱码,和丢失数据,.h,.so都会出现,暂未发现丢包现象
    qte搭建
    移植tslib
    网页移植前的问题(包括触摸屏)
    网页移植开发板
    开发板boa移植
    dump工具下,资料的备份与还原
    python-时间模块
  • 原文地址:https://www.cnblogs.com/udld/p/4193585.html
Copyright © 2011-2022 走看看