大于非负整数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,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 输出: 解释: 各位相加的过程为: + = , + = . 由于 是一位数,所以返回 . 进阶:你可以 ...
随机推荐
- printk一些技巧【转】
转自:http://haohetao.iteye.com/blog/1147791 转自:http://blog.csdn.net/wbd880419/article/details/73530550 ...
- [c++,bson] linux 使用 BSON 编程[www]
[c++,bson] linux 使用 BSON 编程 http://blog.chinaunix.net/uid-28595538-id-4987410.html 1.js db=db.getSib ...
- bzoj 1179 Atm
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1179 题解: 一道比较综合的图论题 直接讲正解: 如果这个图G中存在某个强连通分量,那么这 ...
- mysql utf8改utf8mb4
由于需要用到utf8mb4,之前是utf8现在给改成utf8mb4 查看当前环境 SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' ...
- Cent os FTP配置
原文:http://www.aicoffees.com/itshare/412261137.html
- 几条学习python的建议
熟悉python语言, 以及学会python的编码方式. 熟悉python库, 遇到开发任务的时候知道如何去找对应的模块. 知道如何查找和获取第三方的python库, 以应付开发任务. 学习步骤 安装 ...
- MiCode108 猜数字
Description 相传,十八世纪的数学家喜欢玩一种猜数字的小游戏,规则如下: 首先裁判选定一个正整数数字 N (2 \leq N \leq 200)N(2≤N≤200),然后选择两个不同的整数X ...
- 爬虫基础库之requests
requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...
- matlab安装及使用
matlab R2015b在ubuntu 14.04环境下的安装 挂载及运行安装程序 sudo mkidr /media/matlab mount -o loop matlab_R2015b.iso ...
- 树莓派使用opencv
安装 reference1 reference2 注意 安装顺利,但是使用的时候提示 you need install libgtk2.0-dev xxx ,这时候说明你安装的库的顺序不对,你应该先安 ...