两道华为在线编程题

为啥要写这个,挺郁闷的今天,和朋友一起在做华为在线编程题,够折腾的。第一题输出到底有没有空格,简直了,第二题映射表太长,第三题不记得了。
1、输出100至1000以内的水仙花数

public static void main(String[] args) {
    int a = 0, b = 0, c = 0;
    int count = 0;
    int sum = 0;
    for (int i = 100; i < 1000; i++) {
        a = i / 100;
        b = i / 10 % 10;
        c = i % 10;
        if (i == (a * a * a + b * b * b + c * c * c)) {
            count++;
            System.out.println("第" + count + "个水仙花数:" + i);
            sum = sum + i;
        }
    }
    System.out.println("水仙花数总和:" + sum);
}

2、假设电报中用(.)表示1,用中划线(-)表示0,点与中划线的序列,能够编译成一个二进制数(可以看做无符号数)。二进制数转换成整数,映射表对应英文字母。多个点、中划线间用#隔开(多个连续#号算作一个) 电报没有点、中划线只有#时电报内容为空,二进制数值超过映射表输出“ERROR” 具体映射标见代码中

import java.util.HashMap;
import java.util.Scanner;

public class Decode {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        char[] sInput = s.toCharArray();// 输入电报字符数组
        // 映射表 (不连续的 没规律 看一个输入一个)
        HashMap map = new HashMap();
        map.put(0, "F");
        map.put(1, "G");
        map.put(2, "R");
        map.put(3, "S");
        map.put(4, "T");
        map.put(5, "L");
        map.put(11, "W");
        map.put(12, "X");
        map.put(13, "Y");
        map.put(14, "Z");
        map.put(15, "U");
        map.put(16, "A");
        map.put(22, "B");
        map.put(23, "C");
        map.put(24, "D");
        map.put(25, "E");
        map.put(30, "p");
        map.put(31, "j");
        map.put(26, "l");
        map.put(27, "m");
        map.put(28, "n");
        map.put(29, "o");
        map.put(41, "e");
        map.put(42, "q");
        map.put(37, "a");
        map.put(38, "b");
        map.put(39, "c");
        map.put(40, "d");
        map.put(48, "s");
        map.put(49, "t");
        map.put(50, "u");
        map.put(51, "v");

        int i = 0;
        int al = 0;
        int n = sInput.length;
        String[] a = new String[n];

        while (i < n) {//解析输入电报
            while (i < n && sInput[i] == '#') {
                i++;
            }
            while (i < n && sInput[i] != '#') {
                if (sInput[i] == '-' || sInput[i] == '.') {
                    a[al] = a[al] + sInput[i];
                }
                i++;
            }
            al++;
        }

        if (al == 1) {
            System.out.println("");
            return;
        }

        int[] num = new int[al - 1];

        for (i = 0; i < num.length; i++) {
            num[i] = toNum(a[i]);//转换成映射表数字
        }

        String result = "";
        for (i = 0; i < num.length; i++) {
            if (map.containsKey(num[i])) {
                result = result + map.get(num[i]);
            } else {
                System.out.println("ERROR");
                return;
            }

        }

        System.out.println(result);
    }

    public static int toNum(String s) {
        int num = 0;
        int temp = 0;
        char[] chs = s.toCharArray();

        if (chs[0] == '.')
            temp = 1;
        num = temp;
        for (int i = 1; i < chs.length; i++) {
            num = num * 2;
            if (chs[i] == '.')
                temp = 1;
            else
                temp = 0;
            num = num + temp;
        }

        return num;
    }
}

也不知道有没有bug,只记得这些了,火速分享……

你可能感兴趣的:(java,java,编程,华为,华为优招)