4.18美团笔试 前三道

1. 小美的热搜

public class ex1 {
    static List<Map.Entry<Integer, Integer>> list = new LinkedList<>();
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String ns = in.nextLine();
            int n = Integer.parseInt(ns);
            for (int i = 0; i < n; i++) {
                String s = in.nextLine();
                String[] s1 = s.split(" ");
                if (s1.length == 1) {
                    news(s1[0]);
                } else {
                    news(s1[0], Integer.parseInt(s1[1]), Integer.parseInt(s1[2]));
                }
            }
        }
    }

    public static void news(String query) {
        if (list.size() == 0) {
            System.out.println("null");
        }
        if (list.size() <= 10) {
            for (Map.Entry<Integer, Integer> entry : list) {
                System.out.print(entry.getKey() + " ");
            }
            System.out.println();
        } else {
            for (int i = 0; i < 10; i++) {
                System.out.print(list.get(i).getKey() + " ");
            }
            System.out.println();
        }

    }

    public static void news(String append, int x, int y) {
        int flag = 0;
        for (Map.Entry<Integer, Integer> integerIntegerEntry : list) {
            if (x == integerIntegerEntry.getKey()) {
                integerIntegerEntry.setValue(integerIntegerEntry.getValue() + y);
                flag = 1;
                break;
            }
        }
        if (flag == 0) {
            list.add(new AbstractMap.SimpleEntry<>(x, y));
        }
        Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                return o2.getValue() - o1.getValue() == 0 ? o1.getKey() - o2.getKey() : o2.getValue() - o1.getValue();
            }
        });
    }
}

2.小美的士兵

import java.util.Scanner;
public class ex2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int k = in.nextInt();

        int[][] A = new int[n][2];
        long sumA = 0;
        int[][] B = new int[m][2];
        long sumB = 0;
        for (int i = 0; i < n; i++) {
            A[i][0] = in.nextInt();
            A[i][1] = in.nextInt();
            if (A[i][1] >= k) {
                sumA += A[i][0] * A[i][1];
            }
        }
        for (int i = 0; i < m; i++) {
            B[i][0] = in.nextInt();
            B[i][1] = in.nextInt();
            if (B[i][1] >= k) {
                sumB += B[i][0] * B[i][1];
            }
        }
        System.out.println(sumA + " " + sumB);
        System.out.println((sumA > sumB) ? "A" : "B");
    }
}

3. 小美拉火车

import java.util.*;

public class ex3 {
    static List<String> list = new LinkedList<>();
    static Map<String, List<String>> map = new LinkedHashMap<>();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int N = Integer.parseInt(in.nextLine());
            for (int i = 0; i < N; i++) {
                String[] s = in.nextLine().split(" ");
                int ser = Integer.parseInt(s[0]);
                String x = s[1];
                String y = s[2];
                method(ser, x, y);
            }
            for (String s : list) {
                for (String s1 : map.get(s)) {
                    System.out.print(s1 + " ");
                }
            }
        }
        in.close();
    }

    public static void method(int i, String x, String y) {

        if (i == 1) {
            List<String> mapVal = map.get(y);
            if (mapVal == null) {
                list.add(y);
                List<String> temp = new LinkedList<>();
                temp.add(x);
                map.put(y, temp);
            } else {

                mapVal.add(x);
            }
        } else if (i == 2) {
            int x_index = list.indexOf(x);
            int y_index = list.indexOf(y);
            list.set(x_index, y);
            list.set(y_index, x);
        }


    }
}
#笔经##美团##笔试题目##Java工程师#
全部评论
楼主你好,请问你是什么岗位?开发的话,是Java方向还是C++方向?或者其他语言方向~
点赞 回复 分享
发布于 2021-04-18 14:01
楼主 在哪可以补题呀
点赞 回复 分享
发布于 2021-04-19 13:59
同样的前三道题,除了第三个火车没怎么看懂用什么数据结构,前两道题一个HashMap,重写一下排序就行另一个士兵的更简单点,但是考试紧张排序Comparator没写出来..而且最离谱的那个什么网站,提交代码的ac率0%....感觉自己像个**,之前进入看到都是Scanner,我也这样写为什么一个例子都ac不了...?
点赞 回复 分享
发布于 2021-04-19 14:25

相关推荐

评论
4
13
分享
牛客网
牛客企业服务