题解 | #进制转换#

进制转换

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();
    }
}
全部评论

相关推荐

04-29 22:35
门头沟学院 Java
牛友说改了名字能收到offer:旧图新发查看图片
点赞 评论 收藏
分享
吴offer选手:HR:我KPI到手了就行,合不合适关我什么事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务