zoukankan      html  css  js  c++  java
  • 进制转换

    题目描述

    写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )

    输入描述:

    输入一个十六进制的数值字符串。

    输出描述:

    输出该数值的十进制字符串。

    输入例子:
    0xA
    输出例子:
    10
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while(in.hasNext()) {
                String str = in.nextLine();
                System.out.println(convert(str.substring(2)));
            }
        }
        
        private static int convert(String str) {
            int count = 0;
            int temp = 0;
            int n = 0;
            char ch;
            /*
             * 将字符转换为数字
             */
            while(count < str.length()) {
                ch = str.charAt(str.length() - count - 1);
                if(ch >= '0' && ch <= '9')  {
                    temp = ch - '0';
                } else if(ch >= 'A' && ch <= 'Z') {
                    temp = ch - 'A' + 10;
                } else if(ch >= 'a' && ch <= 'z') {
                    temp = ch - 'a' + 10;
                } else {
                    break;
                }
                
                n += temp * Math.pow(16, count);  // 按公式计算
                count ++;
            }
            return n;
        }
    }
  • 相关阅读:
    hdu 4864 Task
    hdu 1501 Zipper
    hdu 1428 漫步校园
    hdu 1505 City Game
    hdu 1337 The Drunk Jailer
    9-13记录
    python 读取unicode编码文件
    梯度出现Nan值的追踪
    Rstudio-server更改R版本
    stdout/stderr作用学习
  • 原文地址:https://www.cnblogs.com/zywu/p/5805859.html
Copyright © 2011-2022 走看看