lzy1227 1 month ago
parent 86b0dfdc58
commit 7ccb170c95

@ -22,31 +22,6 @@ public class computer {
}
// 将十进制数转换为R进制数
public static String decimalToBase(int num, int r) {
if (num == 0) {
return "0";
}
StringBuilder result = new StringBuilder();
while (num > 0) {
int remainder = num % r;
if (remainder < 10) {
result.append(remainder); // 数字字符
} else {
result.append((char) ('A' + (remainder - 10))); // 字母字符
}
num /= r;
}
return result.reverse().toString(); // 反转结果
}
// 将任意R进制数X从进制R_from转换到进制R_to
public static String convertBase(String x, int rFrom, int rTo) {
int decimalValue = baseToDecimal(x, rFrom); // 转换为十进制
return decimalToBase(decimalValue, rTo); // 转换为目标进制
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

Loading…
Cancel
Save