进制转换(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);
    }
}


全部评论

相关推荐

牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
评论
17
收藏
分享

创作者周榜

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