這個(gè)題是之前在別的學(xué)校比賽練習(xí)系統(tǒng)上遇到過(guò)的,之前好像沒(méi)有說(shuō)有超過(guò)十進(jìn)制的。當(dāng)時(shí)不能提交了,但是寫(xiě)出來(lái)了。
代碼(十六進(jìn)制的沒(méi)有考慮進(jìn)去):
import java.util.Scanner;public class B { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int k=sc.nextInt(); String num=sc.nextInt()+""; boolean fa=getBool(num); int conut=0; while(fa==false){ String str=getAdd(k,num); num=str; fa=getBool(str); conut++; if(conut==30){ System.out.PRintln("Impossible!"); return; } } System.out.println("STEP="+conut); } private static boolean getBool(String str) { int le=str.length(); for (int i = 0; i < le/2; i++) { if(str.charAt(i)!=str.charAt(le-i-1)){ return false; } } return true; } //K進(jìn)制的加法 private static String getAdd(int k,String num) { String str=""; int jw=0; int le=num.length()-1; for (int i = num.length()-1; i >= 0; i--) { int x=(num.charAt(i)+num.charAt(le-i))-2*'0'+jw; str=x%k+str; jw=x/k; } if(jw==1) str=1+str; return str; }}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注