解题思路:StringBuffer类可以执行反序操作;Integer类可以灵活在字符串和n进制数之间进行转换。
注意事项:
参考代码:
import java.util.Scanner; public class Main { public static boolean isHuiwen(String number) { StringBuffer str = new StringBuffer(number); String number_ = str.reverse().toString(); if(number.equals(number_)) return true; else return false; } public static String nSystemAdd(String number, int numSystem) { String sum; int number1 = Integer.parseInt(number, numSystem);//将number转化为十进制 StringBuffer str = new StringBuffer(number+""); int number2 = Integer.parseInt(str.reverse().toString(), numSystem);//得到number反序数的十进制 sum = Integer.toString(number1+number2, numSystem); return sum; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int numSystem, count = 0; String number; numSystem = in.nextInt(); number = in.next(); in.close(); while(count <= 30) { if(isHuiwen(number)) { System.out.println("STEP="+count); break; } else { number = nSystemAdd(number, numSystem); count++; } } if(count == 31) System.out.println("Impossible!"); } }
0.0分
0 人评分