Java-进制转换

描述:
进制范围2-62,每个数字的可取值范围是0-9a-zA-Z
输入原进制,转换后进制,待转换的整数值。
输出转换后的值。
思路:
定义字符串存储0-9a-zA-Z即可

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String nums = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        String input = scanner.next();
        int result = 0;
        for (int i = input.length() - 1; i >= 0; i--) {
            char c = input.charAt(i);
            result = result + (int) (nums.indexOf(c) * Math.pow(a, input.length() - 1 - i));
        }
        StringBuffer sb = new StringBuffer();
        while ((result / b) != 0) {
            int temp = result % b;
            sb.insert(0, nums.charAt(temp));
            result = result / b;
        }
        sb.insert(0, nums.charAt(result));
        System.out.println(sb);

    }
}


posted @ 2017-08-23 21:32  浩月白雪  阅读(161)  评论(0编辑  收藏  举报