大于非负整数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,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 输出: 解释: 各位相加的过程为: + = , + = . 由于 是一位数,所以返回 . 进阶:你可以 ...
随机推荐
- Vue组件-组件的事件
自定义事件 通过prop属性,父组件可以向子组件传递数据,而子组件的自定义事件就是用来将内部的数据报告给父组件的. <div id="app3"> <my-com ...
- 大原則 研讀 spec 與 code 的 心得
最近在研究 stm32f429i-disc0 的 device tree source code, 並且 參造 Devicetree Specification Release 0.1, 在 dts ...
- codevs 1038 一元三次方程求解 NOIP2001提高组
题目链接:http://codevs.cn/problem/1038/ 题解: 嗯,exm?才知道二分隶属搜索专题…… 对-100到100枚举,按照题目中的提示,当当fi*fi+1<0时,二分深 ...
- Linux系统编程——进程间通信(一)
基本操作命令: ps -ajx/-aux/-ef 查看进程间状态/的相互关系 top 动态显示系统中的进程 nice 按照指定的优先级运行 /renice 改变正在运行的进程的优先级 kill -9杀 ...
- ueditor在QQ浏览器或者IE浏览器中无法加载
因为IE浏览器有兼容问题,打开网址,浏览器不一定以最新的文档模式加载.按F12查看 在你网址的head标签中加入:<meta http-equiv="x-ua-compatible&q ...
- maven repository 配置
eclipse maven 配置修改: maven repository 配置 http://blog.csdn.net/joewolf/article/details/4876604 Maven缺省 ...
- Intellij idea的maven依赖图
Intellij idea下查看maven的依赖图与eclipse有所不同.下面简单介绍一下Intellij下maven的查看使用. 使用场景 当你想查看maven依赖的jar都有哪些,是否有冲突,冲 ...
- Linux 基础——处理文件与目录的命令
继续第三天学习,每天下班后积累一点点,始终相信厚积薄发. 一.处理文件的命令 touch dest_file:在当前目录下创建指定的文件. cp source dest:将指定的猿文件复制到目标文件, ...
- (2)ansible主机清单文件inventory
1)inventory作用 作用:通常用于定义要管理主机的认证信息,例如ssh登录用户名,密码等相关信息 缺省文件:/etc/ansible/hosts 2)定义主机组方式 #vim /etc/ans ...
- 基于Shiro,JWT实现微信小程序登录完整例子
小程序官方流程图如下,官方地址 : https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html ...