思路之一:
1、如何得到最高位和最低位数字? 利用 % 和 / 运算
2、如何去除最高位和最低位数字? 利用 % 和 / 运算
3、循环
4、看代码注释
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(bf.readLine());
if(isHw(n)){
System.out.print("Yes");
}else{
System.out.print("No");
}
}
public static boolean isHw(int n){
//如果是负数最小值直接返回false
//负数最小值的绝对值会越界
if(n==Integer.MIN_VALUE){
return false;
}
if(n>-10&&n<10){
return true;
}
int num =n;
if(n<0){
//负数变正数
num = -n;
}
int help = 10;
//求与num的相同位数的help
while(num/help>=10){
help = help*10;
}
while(num>0){
//最高位数字
int left = num/help ;
//最低位数字
int right = num%10;
if(left!=right){
return false;
}
//去除最高位和最低位后的值
num = (num%help)/10;
//help位数减2,保持与num相同的位数
help = help/100;
}
return true;
}
}
京公网安备 11010502036488号