题解 | #进制转换#

进制转换

http://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6

只需要按照16进制转10进制的方法写代码实现就行了,具体如下:


public class Main {
    private static int[] num = {10, 11, 12, 13, 14, 15, 16};
    private static int[] poe = {0, 1, 2};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int res = 0;
        while (in.hasNextLine()) {
            String str = in.nextLine();
            int len = str.length();
            for (int i = len - 1; i > 1 ; i --) {
                if (str.charAt(i) >= 'A' && str.charAt(i) <= 'F') {
                    res += num[str.charAt(i) - 'A'] * Math.pow(16, len - 1 - i);
                } else {
                    res += Integer.valueOf(str.charAt(i) - '0') * Math.pow(16, len - 1 - i);
                }
            }
        }
        System.out.println(res);
    }
}
全部评论
哦对,本来是要用poe数组来表示次方的,但是换了写法没用上, 忘记删了
点赞
送花
回复 分享
发布于 2022-05-23 16:37

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务