题解 | #进制转换#

进制转换

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

import java.util.HashMap;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
        public static void main(String[] args) {
        HashMap<Character, Integer> numMapper = new HashMap<Character, Integer>() {
            {
                put('0', 0);
                put('1', 1);
                put('2', 2);
                put('3', 3);
                put('4', 4);
                put('5', 5);
                put('6', 6);
                put('7', 7);
                put('8', 8);
                put('9', 9);
                put('A', 10);
                put('B', 11);
                put('C', 12);
                put('D', 13);
                put('E', 14);
                put('F', 15);
            }
        };
        Scanner in = new Scanner(System.in);
        String ox = in.nextLine().substring(2).toUpperCase();
        // 注意 hasNext 和 hasNextLine 的区别
        int scale = 1;
        int sum = 0;
        for (int i = ox.length() - 1; i >= 0; i--) {
            sum += numMapper.get(ox.charAt(i)) * scale;
            scale *= 16;
        }
        System.out.println(sum);
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-21 13:41
点赞 评论 收藏
分享
半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务