进制转换(Map)

进制转换

https://www.nowcoder.com/practice/8f3df50d2b9043208c5eed283d1d4da6?tpId=37&tqId=21228&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=

刷题日志,凡分享出来的题目皆为本人独立完成。水平一般,能力有限,但还是希望能给看到的有缘人带来一丝启发。
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String ss = s.substring(2);
        int n = ss.length();
        int j = 1;
        int sum = 0;
        Map<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);
        for(int i = n - 1 ; i >= 0 ; i--){
            int h = map.get(ss.charAt(i));
            sum += h * j;
            j *= 16;
        }
        System.out.println(sum);
    }
}


全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 12:10
点赞 评论 收藏
分享
评论
17
收藏
分享

创作者周榜

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