16进制转10进制简单写法

进制转换

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

为什么我的代码运行时长这么长?

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            char[] ch = str.toCharArray();
            int res = 0;
            for(int i = 2;i<ch.length;i++){
                   int num ;
                if('A'<=ch[i]&&ch[i]<='F'){
                   num = ch[i]-'A'+10; 
                }else{
                    num = ch[i]-'0';
                }
                res = res*16+num;
            }
            System.out.println(res);
        }
    }
}
全部评论

相关推荐

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