题解 | #进制转换#

进制转换

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

Scanner sc = new Scanner(System.in);
    int res = 0; //定义最终值
    while(sc.hasNextLine()){
      String str = sc.nextLine();
      String s1 =str.substring(2); //截取字符串,省的在下标上晕
      System.out.println(s1);
      for(int i=s1.length()-1;i>=0;--i){ //从右到左开始计算
         char c = s1.charAt(i);
         int num = 0;  //记录每一位的十进制值
         if(c>='0' && c<='9')  //切忌一定要 >= <=
            num = c-'0';   //char类型 转换成int技巧
         else if( c>='A' && c<='F'){
           num = c-'A'+10;
           System.out.println(num);
         }
         else if( c>='a' && c<='f')
           num = c-'a'+10;

         res = (int) (res + num* Math.pow(16,s1.length()-i-1));
        System.out.println(res);
      }
      System.out.println(res);
    }

全部评论

相关推荐

我就是0offer糕手:北大不乱杀
点赞 评论 收藏
分享
野猪不是猪🐗:我assume that你must技术aspect是solid的,temperament也挺good的,however面试不太serious,generally会feel style上不够sharp
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务