题解 | #进制转换#

进制转换

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

import java.util.Scanner;
public class Main{
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        int num = 0;
        int factorial = 1;
        if(str.startsWith("0x") == true || str.startsWith("0X") == true){
            for(int i = str.length() - 1; i > 1; i--){
                int temp = (int)str.charAt(i);
                if(97 <= temp && temp <= 102){
                    num += (temp - 87) * factorial;
                }
                else if(65 <= temp && temp <= 70){
                    num += (temp - 55) * factorial;
                }    
                else if(48 <= temp && temp <= 57){
                    num += (temp - 48) * factorial;
                }
                else{
                   System.out.println("Error");
                }
                factorial *= 16;
            }
            System.out.print(num);
        }
        in.close();
    }
}
全部评论

相关推荐

ddd7_:跟我一模一样,加微信的hr都同一个,扫码了白年书人查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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