华为机考-简单-第二次提交

HJ8 合并表记录

数据表记录包含表索引index和数值value(int范围的正整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照index值升序进行输出。

  Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        HashMap<Integer, Integer> hashMap = new HashMap<>();

        for (int i1 = 0; i1 < i; i1++) {
            int k = scan.nextInt();
            int v = scan.nextInt();
            if (hashMap.containsKey(k)) {
                hashMap.put(k,hashMap.get(k)+v);
            }else {
                hashMap.put(k,v);
            }
        }
        hashMap.keySet().stream().sorted().forEach(e ->{
            System.out.println(e+" "+hashMap.get(e));
        });

HJ10 字符个数统计

编写一个函数,计算字符串中含有的不同字符的个数。字符在 ASCII 码范围内( 0~127 ,包括 0 和 127 ), 换行表示结束符,不算在字符里。不在范围内的不作统计。多个相同的字符只计算一次例如,对于字符串 abaca 而言,有 a、b、c 三种不同的字符,因此输出 3 。

 Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        char[] chars = s.toCharArray();
        HashMap<Character, Integer> hashMap = new HashMap<>();
        for (char aChar : chars) {
            hashMap.put(aChar,0);
        }
        System.out.println(hashMap.keySet().size());

HJ11 数字颠倒

输入一个整数,将这个整数以字符串的形式逆序输出程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001


        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        StringBuilder stringBuilder = new StringBuilder(s);
        System.out.println(stringBuilder.reverse());

HJ12 字符串反转

接受一个只包含小写字母的字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)

		Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        StringBuilder stringBuilder = new StringBuilder(s);
        System.out.println(stringBuilder.reverse());

HJ13 句子逆序

将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符

  Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        String[] s1 = s.split(" ");
        for (int i = s1.length-1; i >= 0; i--) {
            System.out.print(s1[i] + " ");
        }

HJ14 字符串排序

给定 n 个字符串,请对 n 个字符串按照字典序排列。

			 Scanner in = new Scanner(System.in);
            int i = in.nextInt();
            String  []  str = new String[i];
            for (int i1 = 0; i1 < i; i1++) {
                str[i1] = in.next();
            }

            Arrays.sort(str);

            for (String s : str) {
                System.out.println(s);
            }

HJ15 求int型正整数在内存中存储时1的个数

输入一个 int 型的正整数,计算出该 int 型数据在内存中存储时 1 的个数。

        Scanner in = new Scanner(System.in);
        int i = in.nextInt();
        System.out.println(Integer.bitCount(i));

HJ21 简单密码

现在有一种密码变换算法。

九键手机键盘上的数字与字母的对应: 1–1, abc–2, def–3, ghi–4, jkl–5, mno–6, pqrs–7, tuv–8 wxyz–9,

0–0,把密码中出现的小写字母都变成九键键盘对应的数字,如:a 变成 2,x 变成 9.

而密码中出现的大写字母则变成小写之后往后移一位,如:X ,先变成小写,再往后移一位,变成了 y ,例外:Z 往后移是 a 。数字和其它的符号都不做变换。

        Scanner scanner = new Scanner(System.in);
        char[] chars = scanner.nextLine().toCharArray();
        HashMap hashMap = hashCopy();
        for (char aChar : chars) {
            Pattern compile = Pattern.compile("[^0-9]+");
            Matcher matcher = compile.matcher(String.valueOf(aChar));
            if (matcher.matches()) {
                if (Character.isUpperCase(aChar)) {
                    if ('Z' == aChar) {
                        System.out.print('a');
                    } else {
                        System.out.print(Character.toChars(Character.toLowerCase(aChar) + 1));
                    }
                } else {
                    //将小写字母转1-9
                    System.out.print(hashMap.get(aChar));
                }
            } else {
                System.out.print(aChar);
            }
        }
            public static HashMap hashCopy() {
        // abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9,
        HashMap<Character, Integer> map = new HashMap<>();
        map.put('a', 2);
        map.put('b', 2);
        map.put('c', 2);

        map.put('d', 3);
        map.put('e', 3);
        map.put('f', 3);

        map.put('g', 4);
        map.put('h', 4);
        map.put('i', 4);

        map.put('j', 5);
        map.put('k', 5);
        map.put('l', 5);

        map.put('m', 6);
        map.put('n', 6);
        map.put('o', 6);

        map.put('p', 7);
        map.put('q', 7);
        map.put('r', 7);
        map.put('s', 7);

        map.put('t', 8);
        map.put('u', 8);
        map.put('v', 8);

        map.put('w', 9);
        map.put('x', 9);
        map.put('y', 9);
        map.put('z', 9);

        return map;
    }

HJ22 汽水瓶

某商店规定:三个空汽水瓶可以换一瓶汽水,允许向老板借空汽水瓶(但是必须要归还)。小张手上有n个空汽水瓶,她想知道自己最多可以喝到多少瓶汽水。

   Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int bottle = sc.nextInt();
            if (bottle == 0) {
                break;
            }
            System.out.println(bottle / 2);
        }

HJ23 删除字符串中出现次数最少的字符

实现删除字符串中出现次数最少的字符,若出现次数最少的字符有多个,

则把出现次数最少的字符都删除。输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序。

  Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        char[] chars = s.toCharArray();
        HashMap<Character, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            hashMap.put(chars[i],hashMap.getOrDefault(chars[i],0)+1);
        }
        Integer min = Collections.min(hashMap.values());

        for (Character character : hashMap.keySet()) {
            if (hashMap.get(character) == min) {
                s = s.replaceAll(String.valueOf(character),"");
            }
        }

        System.out.println(s);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zcy_code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值