题意理解
这题的难点大概在于想到用短除法,且余数为正数。
设 x/y=a⋯b ,则有 x=ay+b 。当 b<0 时,由于题目中 y<0 ,因此可以调整成 x=(a+1)y+(b−y)
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String symbolTable = "0123456789ABCDEFGHIJKLMN";
int x = scanner.nextInt();
int y = scanner.nextInt();
String res = "";
int temp = x;
while(x != 0) {
int t = x % y;
if(t >= 0) {
x = x / y;
} else {
t -= y;
x = x / y + 1;
}
res = symbolTable.charAt(t) + res;
}
System.out.println(temp + "=" + res + "(base" + y + ")");
scanner.close();
}
}
欢迎加入“不会算法一群菜鸟”,群号是⑥⑥①⑨②2025,这是我设置的一道很低的门槛用来阻止广告的。入群的验证暗号是:我爱编译原理