题解 | #进制转换#

进制转换

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (sc.hasNext()) { // 注意 while 处理多个 case
            String str=sc.nextLine();
            //十六进制字符串如何改为十进制,好像是每个单位*16的几次饭
            String num=str.substring(2);//前两位不算
            int result=0;
            int power=1;
            for(int i=num.length()-1;i>=0;i--)
            {
                char c=num.charAt(i);
                if(c>='0'&&c<='9')
                {
                    result+=(c-'0')*power;
                }
                else if(c>='A'&&c<='F')
                {
                    result+=(c-'A'+10)*power;
                }
                power*=16;
            }
            System.out.println(result);
        }
        sc.close();
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 11:16
点赞 评论 收藏
分享
不亏是提前批,神仙打架,鼠鼠不配了
站队站对牛:现在92都报工艺岗了
投递韶音科技等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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