华为笔试题(3)

来源:互联网 发布:药妆 知乎 编辑:程序博客网 时间:2024/06/05 10:24

一、
输入描述:
输入候选人的人数,第二行输入候选人的名字,第三行输入投票人的人数,第四行输入投票。
输出描述:
每行输出候选人的名字和得票数量。
输入
4
A B C D
8
A B C D E F G H
输出
A : 1
B : 1
C : 1
D : 1
Invalid : 4

public class Main{    public static void main(String[] args) {        Scanner sc=new Scanner(System.in);        //直接用LinkedHashMap统计票数即可,因为输出顺序和输入顺序相同,用Linked        while(sc.hasNextInt()) {            int n = sc.nextInt();            Map<String,Integer> candidate = new LinkedHashMap<>();            for(int i = 0; i < n; i++) {                candidate.put(sc.next(), 0);            }            int m = sc.nextInt();            int cnt = 0;            for(int i = 0; i < m; i++) {                String tmp = sc.next();                if(candidate.containsKey(tmp)) {                    candidate.put(tmp, candidate.get(tmp) + 1);                    cnt++;                }            }            for(String s : candidate.keySet()) {                System.out.println(s+" : "+candidate.get(s));            }            System.out.println("Invalid : "+(m - cnt));        }        sc.close();    }}

二、
编写一个函数,传入一个int型数组,返回该数组能否分成两组,使得两组中各元素加起来的和相等,
并且,所有5的倍数必须在其中一个组中,所有3的倍数在另一个组中(不包括5的倍数),
能满足以上条件,返回true;不满足时返回false。
输入描述:
第一行是数据个数,第二行是输入的数据
输出描述:
返回true或者false
示例1
输入
4
1 5 -5 1
输出
true

public class Main{    public static void main(String[] args) {        Scanner sc=new Scanner(System.in);        /* 全排列问题,可以用递归         * 能被5整除的放一组,被3整除的放一组         * 剩余元素放入两个数组中         * 考虑所有可能情况         */        while(sc.hasNextInt()) {            int n = sc.nextInt();            int five = 0, three = 0, cnt = 0;            int[] a = new int[n];            for(int i = 0; i < n; i++) {                int tmp = sc.nextInt();                if(tmp % 5 == 0) {                    five += tmp;                }else if(tmp % 3 == 0){                    three += tmp;                }else {                    a[cnt++] = tmp;                }            }            System.out.println(equal(five, three, a, 0, cnt));        }        sc.close();    }    private static boolean equal(int five, int three, int[] a, int index, int cnt) {        if(index == cnt) {            if(five == three) {                return true;            }else {                return false;            }        }        if(index < cnt) {            return equal(five + a[index], three, a, index + 1, cnt) ||                    equal(five, three + a[index], a, index + 1, cnt);        }        return false;    }}

三、
输入描述:
输入一个字符串。
输出描述:
输出字符串中最长的数字字符串和它的长度。如果有相同长度的串,则要一块儿输出,但是长度还是一串的长度
输入
abcd12345ed125ss123058789
输出
123058789,9

public class Main{    public static void main(String[] args) {        Scanner sc=new Scanner(System.in);        /* 最长连续数字串问题         * 数字串后加空格,然后切分         */        while(sc.hasNext()) {            String str = sc.next();            StringBuffer sb = new StringBuffer();            int len = str.length();            for(int i = 0; i < len; i++) {                char tmp = str.charAt(i);                if(tmp >= '0' && tmp <= '9') {                    sb.append(tmp);                }else {                    sb.append(" ");                }            }            String[] strs = sb.toString().split(" +");            int max = 0;            for(int i = 0; i < strs.length; i++) {                max = Math.max(max, strs[i].length());            }            for(int i = 0; i < strs.length; i++) {                if(strs[i].length() == max) {                    System.out.print(strs[i]+",");                }            }            System.out.println(max);        }        sc.close();    }}