From 6b186b78b6200a1b0929a2822523b05441aedeeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E8=92=B8?= <3505869163@qq.com> Date: Thu, 17 Oct 2024 14:34:05 +0800 Subject: [PATCH] v1 --- computer/computer.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/computer/computer.java b/computer/computer.java index 790ebab..604be82 100644 --- a/computer/computer.java +++ b/computer/computer.java @@ -22,6 +22,31 @@ 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);