題解1161:【迴文數(二)】 (Java描述)

acDream_發表於2018-06-22

解題思路:

注意事項:居左寫的我覺得有問題,011011二進位制輸入我感覺我是對的

參考程式碼:

public class 迴文數2 {

public static String nv(String num) {

return new StringBuffer(num).reverse().toString();

}

public static boolean isHw(String num) {

String Num = num;

for(int i=0;i<Num.length()/2;i++) {

if(Num.charAt(i)!=Num.charAt(Num.length()-i-1)) {

return false;

}

}

return true;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner in = new Scanner(System.in);

int N = in.nextInt();//輸入幾進位制數

String M = in.next();

int step = 0;

String rs = M;

while(step<=30) {

rs = Integer.toString(Integer.valueOf(rs, N) + Integer.valueOf(nv(rs), N), N);

step++;

if(isHw(rs)) {

break;

}

}

if(step<=30) {

System.out.println("STEP="+step);

}

else {

System.out.println("Impossible!");

}

}


}


相關文章