题解 | #进制转换#

进制转换

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

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String s = sc.nextLine();
            System.out.println(convertJz(s));
        }
    }
    public static Integer convertJz(String str){
        int result = 0;
        if(str==null){
            return result;
        }
        String replace = str.replace("0x", "");
        for(int i = replace.length()-1 ;i>=0;i--){
            double y = Math.pow(16, replace.length()-1-i);
            int x = replace.charAt(i);
            if(x-60>0){
                x = x - 55;
            }else{
                x = Integer.parseInt(replace.charAt(i)+"");
            }
            result += x * (int)y;
        }
        return result;
    }

}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务