题解 | #进制转换#

进制转换

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

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = "";
        while (s.length() == 0) {
            s = in.nextLine().toUpperCase();
        }
        String substring = s.substring(2);
        char[] chars = substring.toCharArray();
        double result = 0;
        int index = 1;
        for (char aChar : chars) {
            result = result + convertNum(aChar) * Math.pow(16d, chars.length - index++);
        }
        System.out.println((int)result);
    }

    public static int convertNum(char s) {
        if (s == 'A') {
            return 10;
        } else if (s == 'B') {
            return 11;
        } else if (s == 'C') {
            return 12;
        } else if (s == 'D') {
            return 13;
        } else if (s == 'E') {
            return 14;
        } else if (s == 'F') {
            return 15;
        }
        return Integer.parseInt(String.valueOf(s));
    }
}

全部评论

相关推荐

高斯林的信徒:问你有没有保底,好人啊,就差把这是kpi面告诉你了
点赞 评论 收藏
分享
群星之怒:不是哥们,你就不好奇瘫痪三十年的老植物人是啥样的吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务