大于非负整数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.代码
import java.util.Scanner;
public class SymmetricNumber {
public static void main(String[] argv){
Scanner in=new Scanner(System.in);
int N=in.nextInt();
String n=String.valueOf(N);
//特殊情况:9999999999999.........
if((N+1)%10==0)
System.out.print(N+2);
//非特殊情况
else
{
if(n.length()==1){
System.out.println(N+1);
}
else{
//偶数位
if(n.length()%2==0){
String temp=n.substring(0,n.length()/2);
String temp_0=n.substring(n.length()/2,n.length());
String temp_1="";
for(int i=n.length()/2-1;i>=0;i--){
temp_1=temp_1+temp.charAt(i);
}
//大于的话则直接输出前半部分和前半部分的倒置
if(Integer.parseInt(temp_1)>Integer.parseInt(temp_0))
System.out.println(temp+temp_1);
//否则前半部分加一,然后新的temp与temp的倒置组成新的String 输出
else
{
temp=String.valueOf(Integer.parseInt(temp)+1); //加一
//本身加倒置组成新的String
for(int i=temp.length()-1;i>=0;i--){
temp=temp+temp.charAt(i);
}
System.out.println(temp);
}
}
//奇数位
else{
String temp_0=n.substring((n.length()+1)/2,n.length());
String temp=n.substring(0,(n.length()+1)/2);
String temp_1="";
for(int i=temp.length()-2;i>=0;i--){
temp_1=temp_1+temp.charAt(i);
}
if(Integer.parseInt(temp_1)>Integer.parseInt(temp_0))
System.out.println(temp+temp_1);
else
{
temp=String.valueOf(Integer.parseInt(temp)+1);
for(int i=temp.length()-2;i>=0;i--){
temp=temp+temp.charAt(i);
}
System.out.println(temp);
}
}
}
}
}
}
大于非负整数N的第一个回文数 Symmetric Number的更多相关文章
- [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number
1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 这个题目也是当时笔试第一次见到,花了一个小时才做出了.慢慢总结还是挺简单的. 2.分析 分析如下: (1)一位数N(9 ...
- [Swift]LeetCode9. 回文数 | Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- Leetcode 9 回文数Palindrome Number
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- [LeetCode] Prime Palindrome 质数回文数
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
- 判断回文字符串、回文链表、回文数(python实现)
所谓回文字符串,就是正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串.即是对称结构 判断回文字符串 方法一: def is_palin ...
- LeetCode(9):回文数
Easy! 题目描述: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: f ...
- LeetCode 5回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全 Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...
- C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数
各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 输出: 解释: 各位相加的过程为: + = , + = . 由于 是一位数,所以返回 . 进阶:你可以 ...
随机推荐
- SIFT四部曲之——高斯滤波
本文为原创作品,未经本人同意,禁止转载 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和http://www.cnblogs.com/xujianqing/ 或 ...
- 以下suse11.3x64可以安装pycrypto-2.6.1
rpm -qa adaptec-firmware-1.35-2.15.4gnome-menus-branding-SLED-11.1-14.26man-pages-3.15-2.23.1crackli ...
- tiny-rtems-src
https://github.com/RTEMS/rtems-libbsd https://github.com/freebsd/freebsd/tree/642b174daddbd0efd9bb5f ...
- glom模块的使用(一)
glom模块的使用 简单说下glom模块主要是处理结构化数据用的,安装简单pip install glom即可,下面就glom的方法参数做例子讲解. glom 和模块同名的glom方法使用方法: .g ...
- JS面试题第一弹
1.javascript的typeof返回哪些数据类型 alert(typeof [1, 2]); //object alert(typeof 'leipeng'); //string ...
- 文字顺时针旋转90度(纵向)&古诗词排版
1.文字旋转90度 width: 100px; height: 200px; line-height: 100px; text-align: center; writing-mode: vertica ...
- django wsgi nginx 配置
""" WSGI config for HelloWorld project. It exposes the WSGI callable as a module-leve ...
- MySQL-5.5.49安装、多实例、主从复制
源码安装mysql yum install ncurses-devel libaio-devel -y mkdir /server/tools -p cd /server/tools wget htt ...
- leetcode 之Partition List(16)
思路就是定义两个链表,一个放大的,一个放小的,最后将两个连起来,注意细节问题. ListNode *partionList(ListNode *head, int value) { ListNode ...
- linux命令(9):route命令
查看路由表:route –n //添加到主机的路由 # route add –host 192.168.168.110 dev eth0 # route add –host 192.168.168.1 ...