10进制转12进制,思考转任意进制

    xiaoxiao2024-11-29  5

    import java.util.ArrayList; import java.util.List; import java.util.Scanner; /* 本题case: input : 23 output: 1B ps:请思考如何变成任意进制的输出,如8进制,14进制等。 */ public class Main12 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); String string = scanner.nextLine(); int n = Integer.parseInt(string); List<Integer> list = new ArrayList<>(); //数组index代表数值,对应12进制的字符 //如10对应A,11对应B String[] iarr = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B" }; int tmp = 0; if (n == 0) { list.add(0); } else { while (n > 0) { tmp = n % 12; list.add(tmp); n = n / 12; } } //反向将结果输出即为正确从左到右,从高位到低位 for (int i = list.size() - 1; i >= 0; i--) { System.out.print(iarr[list.get(i)]); } } }
    转载请注明原文地址: https://ju.6miu.com/read-1294096.html
    最新回复(0)