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

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 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)]);
        }
    }
}

你可能感兴趣的:(算法)