• 13:16进制数值字符串转十进制


    13:

    题目描述

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

    输入描述:

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

    输出描述:

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

    输入例子:

    0xA

    输出例子:

    10

    note:有系统自带的Integer.parseInt(num,16)可以直接转换成对应进制数

    package prctice01;
    
    import java.util.Scanner;
    
    /*13:
    题目描述
    写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )
    输入描述:
    输入一个十六进制的数值字符串。
    输出描述:
    输出该数值的十进制字符串。
    输入例子:
    0xA
    输出例子:
    10*/
    public class Ox2Shi {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            while(scanner.hasNextLine())
            {
                String string  = scanner.nextLine();
                string = string.substring(2);
                System.out.println(Func(string));;
            }
            
        }
    //    public static void main(String[] args) {
    //        Scanner scanner = new Scanner(System.in);
    //        while(scanner.hasNextLine())
    //        {
    //            String string = scanner.nextLine();
    //            string = string.substring(2);
    //            int result = Integer.parseInt(string, 16);
    //            System.out.println(result);
    //        }
    //
    //    }
    
        private static int Func(String string) {
            int count = 0;
            char ch;
            int result = 0;
            int temp = 0;
            while(count < string.length()){
                ch = string.charAt(string.length()-count-1);
                if(ch <='Z' && ch >='A'){
                    temp = ch - 'A' + 10;
                }
                else if(ch <= 'z' && ch >= 'a'){
                    temp = ch - 'a' + 10;
                }
                else if(ch <= '9' && ch >= '0'){
                    temp = ch - '0';
                }
                else break;
                result += temp*Math.pow(16, count);
                count++;
            }
            return result;
        }
    
    }
  • 相关阅读:
    Codeforces Round #276 (Div. 1) E. Sign on Fence 二分+主席树
    Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
    HDU 5918 Sequence I KMP
    HDU 5919 Sequence II 主席树
    hdu 5833 Zhu and 772002 高斯消元
    Codeforces Round #143 (Div. 2) E. Cactus 无向图缩环+LCA
    codeforces 45C C. Dancing Lessons STL
    deeplab hole algorithm
    有时候只是担心多余
    lstm
  • 原文地址:https://www.cnblogs.com/newcoder/p/5764364.html
Copyright © 2020-2023  润新知