题解 | #进制转换#

进制转换

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);
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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