题目:
描述:输入一个正整数,输出距离其最近的对称正整数;如果有多个,则升序输出
示例:103 -> 101
解析:代码有注释,阅读即可明白
程序:
public class FindSymmetryNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
FindSymmetryNumber findSymmetryNumber = new FindSymmetryNumber();
//输入对称数字
if(findSymmetryNumber.isSymmetry(String.valueOf(num))){
System.out.println(num);
}else{
int maxNumber = findSymmetryNumber.findMaxNumber(num);
int minNumber = findSymmetryNumber.findMinNumber(num);
if(minNumber == -1){
System.out.println(maxNumber);
}else {
if(Math.abs(maxNumber-num) == Math.abs(minNumber-num)){
System.out.println(minNumber + " " + maxNumber);
}else {
if(Math.abs(maxNumber-num)>Math.abs(minNumber-num)){
System.out.println(minNumber);
}else {
System.out.println(maxNumber);
}
}
}
}
}
/**
* 给定一个正整数字符串,判断其是否为对称
* @param str
* @return
*/
private Boolean isSymmetry(String str){
int len = str.length();
//数字8不是对称正整数
if(len == 1){
return false;
}
for(int i =0;i<len/2;i++){
if(str.charAt(i) != str.charAt(len-1-i)){
return false;
}
}
return true;
}
/**
* 获取比num大的距离最近的对称整数
* @param num
* @return
*/
private int findMaxNumber(int num){
if(isSymmetry(String.valueOf(num))){
return num;
}else {
return findMaxNumber(++num);
}
}
/**
* 获取比num小的距离最近的对称整数
* @param num
* @return
*/
private int findMinNumber(int num){
//未找到对称正整数字符串
if(num <= 0){
return -1;
}
if(isSymmetry(String.valueOf(num))){
return num;
}else {
return findMinNumber(--num);
}
}
}