题解 | #进制转换#

进制转换

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine();

        string = string.substring(2);
        char[] array = string.toCharArray();

        int res = 0;
        for (int i = 0; i < array.length; i++) {
            int pow = array.length - 1 - i;
            int temp = 0;
            pow = Math.max(pow, 0);

            switch (array[i]){
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    temp = array[i] - '0';
                    break;
                case 'A':
                case 'a':
                    temp = 10;
                    break;
                case 'B':
                case 'b':
                    temp = 11;
                    break;
                case 'C':
                case 'c':
                    temp = 12;
                    break;
                case 'D':
                case 'd':
                    temp = 13;
                    break;
                case 'E':
                case 'e':
                    temp = 14;
                    break;
                case 'F':
                case 'f':
                    temp = 15;
                    break;
            }
            res += (int) (temp * Math.pow(16,pow));
        }
        System.out.println(res);
    }
}

全部评论

相关推荐

求问!考研下岸,打算参加春招,我这个bg能进啥厂,或者需要搞点深度项目再投吗
Java抽象带篮子_...:直接海投,可以看看我的考研失利速成冲春招贴,里面详细写了简历怎么写,学哪些项目可以速成
点赞 评论 收藏
分享
最喜欢秋天的火龙果很...:第一份工作一定要往大的去,工资低点没事。后面换工作会更好找,即使你去小公司,你也不可能不会换工作的。所以找大的去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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