java实现 洛谷 P1017 进制转换-LMLPHP

java实现 洛谷 P1017 进制转换-LMLPHP

import java.util.Scanner;

public class Main {
private static Scanner cin; public static void main(String args[]) throws Exception {
cin = new Scanner(System.in);
int n = cin.nextInt();
int r = cin.nextInt();
int t = n;
int modRet;
StringBuilder sb = new StringBuilder("");
while(t/r != 0 || t%r!=0) {
modRet = t%r;
//如果取模后结果为负数,t/r的结果需要向高位借1
if(modRet < 0) {
t = t/r + 1;
modRet = modRet - r;
}
else {
t = t/r;
}
sb.insert(0, modRet>9?(char)(modRet+55):(char)(modRet+48));
}
sb.insert(0,String.format("%d=", n));
sb.append("(base"+r+")");
System.out.println(sb.toString());
}
}
05-15 21:00