华为机试-HJ21~HJ30

// HJ21 简单密码
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        HashMap<Character, Character> map = new HashMap<>();
        map.put('1', '1');
        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');
        map.put('0', '0');

        StringBuffer sb = new StringBuffer();
        for (char c : line.toCharArray()) {
            if ('A' <= c && c < 'Z') {
                sb.append((char) (c + 33));
            } else if (c == 'Z') {
                sb.append('a');
            } else if (map.get(c) != null) {
                sb.append(map.get(c));
            } else {
                sb.append(c);
            }
        }
        System.out.println(sb.toString());
    }
}

// HJ22 汽水瓶
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { 
            int n = in.nextInt();
            if (n == 0) break;
            int result = 0;
            int k = n / 3;
            int m = n % 3;
            result +=  k;
            if ((k + m) == 2) {
                result += 1;
            }
            while ( (k + m) >= 3 ) {
                int sum = k + m;
                k = sum / 3;
                m = sum % 3;
                result += k;
                if ((k + m) == 2) {
                    result += 1;
                }
            }
            System.out.println(result);
        }
    }
}

// HJ23 删除字符串中出现次数最少的字符
import java.util.*;
public class Main {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        if (line.length() == 1) {
            System.out.println(line);
        }
        
        HashMap<Character, Integer> map = new HashMap<>();
        for (char c : line.toCharArray()) {
            if (!map.containsKey(c)) {
                map.put(c, 1);
            } else {
                map.put(c, map.get(c) + 1);
            }
        }
        ArrayList<te> tes = new ArrayList<>();
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            te te = new te(entry.getKey(), entry.getValue());
            tes.add(te);
        }
        tes.sort((o1, o2) -> {
            if (o1.getCount() > o2.getCount()) {
                return 1;
            }
            if (o1.getCount() == o2.getCount()) {
                return 0;
            }
            return -1;
        });
        ArrayList<Character> delChars = new ArrayList<>();
        Integer minCount = tes.get(0).getCount();
        for (te te : tes) {
            if (Objects.equals(te.getCount(), minCount)) {
                delChars.add(te.getC());
            }
        }

        StringBuffer sb = new StringBuffer();
        for (char c : line.toCharArray()) {
            if (!delChars.contains(c)) {
                sb.append(c);
            }
        }
        System.out.println(sb.toString());
    }

    static class te {
        private Character c;
        private Integer count;

        public te(Character c, Integer count) {
            this.c = c;
            this.count = count;
        }

        public Character getC() {
            return c;
        }

        public void setC(Character c) {
            this.c = c;
        }

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }
    }
}

// HJ26 字符串排序
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        ArrayList<Character> characters = new ArrayList<>();
        for (char aChar : line.toCharArray()) {
            if (('a' <= aChar && aChar <= 'z') || ('A' <= aChar && aChar <= 'Z')) {
                characters.add(aChar);
            }
        }
        characters.sort((o1, o2) -> {
            if ('a' <= o1 && o1 <= 'z') {
                o1 = (char) (o1 - 32);
            }
            if ('a' <= o2 && o2 <= 'z') {
                o2 = (char) (o2 - 32);
            }
            if (o1 > o2) {
                return 1;
            } else if (o1 == o2) {
                return 0;
            } else {
                return -1;
            }
        });
        int index = 0;
        StringBuffer sb = new StringBuffer();
        for (char aChar : line.toCharArray()) {
            if (!(('a' <= aChar && aChar <= 'z') || ('A' <= aChar && aChar <= 'Z'))) {
                sb.append(aChar);
            } else {
                sb.append(characters.get(index++));
            }
        }
        System.out.println(sb.toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值