首页 > 4.18美团笔试 前三道
头像
HandC
编辑于 2021-04-18 20:09
+ 关注

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);
        }


    }
}

全部评论

(3) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐