【算法题】华为2016校园招聘上机笔试题

前言

版权原因题目就不转载了,不过可以看这篇博客:https://blog.csdn.net/qq_36122764/article/details/82499735
这里给出Java对前两道题的解法,由于自己思路不够清晰浪费的时间过长,第三道没有时间做了

#1 最高分是多少

这个题难度不大,但是我最初没有使用要求的循环输入,也就是在外面套一个while (scanner.hasNextInt()),有一点违反题意,总结下教训好好理解题意吧。
另外一个出错点是对于寻找区间内最高分,题目中并没有说明第一个数一定比第二个小,所以不能默认。可以采用if (id >= min && id <= max || id <= min && id >= max)解决。

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int N = scanner.nextInt();
            int M = scanner.nextInt();
            Map<Integer, Integer> scores = new LinkedHashMap<>();
            for (int i = 1; i <= N; i++) {
                scores.put(i, scanner.nextInt());
            }
            for (int i = 0; i < M; i++) {
                String ins = scanner.nextLine();
                if (ins.equals(" ") || ins.equals("")) {
                    i--;
                    continue;
                }
                String[] parts = ins.split(" ");
                if (parts[0].equals("Q")) {
                    int min = Integer.parseInt(parts[1]);
                    int max = Integer.parseInt(parts[2]);
                    int maxScore = 0;
                    for (Integer id : scores.keySet()) {
                        if (id >= min && id <= max || id <= min && id >= max) {
                            int score = scores.get(id);
                            if (score > maxScore)
                                maxScore = score;
                        }
                    }
                    System.out.println(maxScore);
                } else if (parts[0].equals("U")) {
                    int id = Integer.parseInt(parts[1]);
                    int score = Integer.parseInt(parts[2]);
                    scores.put(id, score);
                }
            }
        }
    }
}

#2 简单的错误记录

这道题也不难,不涉及什么数据结构或者算法,但是因为理解偏差与读题不仔细还是踩了很多坑。先说一点,String[] path = parts[0].split("\\");这里的四个斜杠其实代表一个,Java里需要这样转义。另外,对于Map的排序,还是比较常用的:
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
然后是我的一个理解偏差,比如题目里说文件名最多显示16,这个千万不能在读入map时处理,否则会出现多个不同的映射到同一个。另外,最多显示8条,也不说读8条就统计,而是统计完降序最多显示8条。还有就是题目里要求的顺序,如果数量相同以先出现的为准,使用LinkedHashMap而不要HashMap,否则这个顺序无法保证。
总的来说坑还是有的,不仔细很容易卡很久,尽管题不难。

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Map<String, Integer> map = new LinkedHashMap<>();
        while (scanner.hasNextLine()) {
            String error = scanner.nextLine();
            if (error.length() == 0) {
                break;
            }
            String[] parts = error.split(" ");
            String[] path = parts[0].split("\\\\");
            String name = path[path.length - 1];
            String key = name + " " + parts[1];
            map.putIfAbsent(key, 0);
            map.put(key, map.get(key) + 1);
        }
        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
        list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
        int num = list.size() > 8 ? 8 : list.size();
        for (int i = 0; i < num; i++) {
            String key = list.get(i).getKey();
            String name = key.split(" ")[0];
            if (name.length() > 16)
                name = name.substring(name.length() - 16);
            String line = key.split(" ")[1];
            System.out.println(name + " " + line + " " + list.get(i).getValue());
        }
    }
}
发布了75 篇原创文章 · 获赞 28 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Swocky/article/details/97030692