我是靠谱客的博主 舒心云朵,这篇文章主要介绍华为研发工程师编程题——进制转换,现在分享给大家,希望可以做个参考。

一、题目

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

输入描述:

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

输出描述:

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

输入例子1:

0xA

输出例子1:

10

二、代码实现

import java.util.Scanner;

public class BinaryTransform {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            transform(sc.next());
        }
    }

    private static void transform(String input) {
        String str = input.substring(2, input.length());
        int res = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) >= 'A' && str.charAt(i) <= 'F') {
                res += res * 15 + str.charAt(i) - 'A' + 10;
            } else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'f') {
                res += res * 15 + str.charAt(i) - 'a' + 10;
            } else {
                res += res * 15 + str.charAt(i) - '0';
            }
        }
        System.out.println(res);
    }
}

最后

以上就是舒心云朵最近收集整理的关于华为研发工程师编程题——进制转换的全部内容,更多相关华为研发工程师编程题——进制转换内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部