题解 | #进制转换#

进制转换

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


public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        HashMap<Character,Integer> map = new HashMap<>();
        map.put('0',0);
        map.put('1',1);
        map.put('2',2);
        map.put('3',3);
        map.put('4',4);
        map.put('5',5);
        map.put('6',6);
        map.put('7',7);
        map.put('8',8);
        map.put('9',9);
        map.put('A',10);
        map.put('B',11);
        map.put('C',12);
        map.put('D',13);
        map.put('E',14);
        map.put('F',15);
        while(sc.hasNext()){
            long sum = 0;
            String str = sc.next();
            str = str.substring(2);
            for(int i = str.length() - 1; i >= 0 ; i--){
                Set<Map.Entry<Character,Integer>> entry = map.entrySet();
                for(Map.Entry<Character,Integer> e : entry){
                    if(e.getKey() == str.charAt(i)){
                        double a = str.length()-1-i;;
                        double b = 16.0;
                        sum = sum + (long)(e.getValue() * Math.pow(b,a));
                        break;
                    }
                }
            }
            System.out.println(sum);
        }
    }
}
全部评论

相关推荐

牛至超人:哈工大已经很棒了,不需要加括号了,然后咋没有实习经历呢?火速趁寒假整一段实习,导师不让就狠狠肘击
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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