题解 | #进制转换#

进制转换

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while((str=br.readLine())!=null && str.length()>0){
            str = str.replace("0x","");
            int count = str.length();
            String s;
            int q = 0;
            long num=0;
            for(int i=count-1;i>=0;i--){
                s = str.substring(i,i+1);
                if(s.equals("A")){
                    s = "10";
                }
                if(s.equals("B")){
                    s = "11";
                }
                 if(s.equals("C")){
                    s = "12";
                }
                if(s.equals("D")){
                    s = "13";
                }
                 if(s.equals("E")){
                    s = "14";
                }
                if(s.equals("F")){
                    s = "15";
                }
                num += Integer.parseInt(s)*Math.pow(16,q);
                q++;
            }
            System.out.println(num);
        }
    }
}
全部评论

相关推荐

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