美团 2021 届秋季校园招聘笔试真题

美团 2021 届秋季校园招聘笔试真题

小美的用户名

import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        String[] arr = new String[T];
        for (int i = 0; i < T; i++) {
            arr[i] = scanner.next();
        }
        String regex = "^([a-zA-Z])([a-zA-Z]*)([0-9]{1,})([a-zA-Z0-9]*)$";
//        String regex = "^([a-zA-Z])([a-zA-Z]*)([0-9]+)([a-zA-Z]*)$";
//        String regex = "^([a-zA-Z])([a-zA-Z]*)([0-9]+)+([a-zA-Z0-9]*)$";
        for (int i = 0; i < T; i++) {
            System.out.printf("%s\n",arr[i]);
            boolean res = Pattern.matches(regex, arr[i]);
            if (res) System.out.println("Accept");
            else System.out.println("Wrong");
        }

    }

}

小美的跑腿代购

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int n = scanner.nextInt(), m = scanner.nextInt();
    //[0]存编号,[1]存获取该订单编号的价值
    //这里的排序是按价值相同相同时,大的编号排在前面
    //否则按价值从小到大排序,因为下面有个peek 的比较操作
    PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> {
        if (a[1] == b[1]) return b[0] - a[0];
        else return a[1] - b[1];
    });
    for (int i = 0; i < n; i++) {
        int v = scanner.nextInt(), w = scanner.nextInt();
        if (pq.size() < m) pq.offer(new int[]{i, v + w * 2});
        else if (pq.peek()[1] < v + w * 2) {
            pq.poll();
            pq.offer(new int[]{i, v + w * 2});
        }
    }
    int[] res = new int[m];
    for (int i = 0; i < m; i++) {
        res[i] = pq.poll()[0];
    }
    Arrays.sort(res);
    for (int x : res) {
        System.out.print(x + 1 + " ");
    }
}

另外一种写法,全部塞入数组arr后,排序,价值高的排在前面,价值相同,序号小的,排在前面

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(), m = scanner.nextInt();
        Integer[][] arr = new Integer[n][2];
        for (int i = 0; i < n; i++) {
            int v = scanner.nextInt(), w = scanner.nextInt();
            arr[i][0] = i;
            arr[i][1] = v + w * 2;
        }
        Arrays.sort(arr, (a, b) -> {
            if (a[1].equals(b[1])) return a[0] - b[0];
            return b[1] - a[1];
        });
        int[] res = new int[m];
        for (int i = 0; i < m; i++) {
            res[i] = arr[i][0] + 1;
        }
        Arrays.sort(res);
        for (int x : res) {
            System.out.print(x + " ");
        }
    }

小团的复制粘贴

  • 按题意模拟
static final int N = 100010;

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] A = new int[N];
    int[] B = new int[N];
    for (int i = 1; i <= n; i++) {
        A[i] = sc.nextInt();
        B[i] = -1;
    }
    int m = sc.nextInt();
    while (m-- > 0) {
        int op = sc.nextInt();
        if (op == 1) {
            int k = sc.nextInt();
            int x = sc.nextInt();
            int y = sc.nextInt();
            for (int i = x, j = y; i < k + x; i++, j++) {
                B[j] = A[i];
            }
        } else {
            int x = sc.nextInt();
            System.out.printf("%d\n", B[x]);
        }
    }
}

小美的区域会议

static final int MOD = (int) (1e9) + 7;

static int n, k;
static List<List<Integer>> edges;
static int[] level;
static boolean[] vis;


public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    k = sc.nextInt();
    edges = new ArrayList<>();
    level = new int[n + 1];//级别
    vis = new boolean[n + 1];//访问数组
    for (int i = 0; i <= n; i++) {
        edges.add(new ArrayList());
    }
    for (int i = 1; i < n; i++) {
        int a = sc.nextInt();
        int b = sc.nextInt();
        edges.get(a).add(b);
        edges.get(b).add(a);
    }
    for (int i = 1; i <= n; i++) {
        level[i] = sc.nextInt();
    }
    long res = 0;
    for (int i = 1; i <= n; i++) {
        Arrays.fill(vis, false);//每一轮都初始化
        res = (res + dfs(i, i)) % MOD;
    }
    System.out.println(res);
}


private static long dfs(int u, int s) {
    vis[u] = true;//标记已经被访问过
    long res = 1;
    for (int v : edges.get(u)) {
        if (vis[v]) continue;
        //两种情况:1.当前v的级别应该大于s的级别,但是级别差距控制在k以内
        //2.相同级别时,按从小到大开始遍历
        if (level[v] > level[s] && level[v] - level[s] <= k
                || level[v] == level[s] && v > s) {
            res = res * (1 + dfs(v, s)) % MOD;
        }
    }
    return res;
}

小团的神秘暗号

请添加图片描述

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    String T = sc.next();
    char[] chas = T.toCharArray();
    int l = 0, r = chas.length - 1;
    for (; l < chas.length; l++) {//头部出现过'M'
        if (chas[l] == 'M') {
            l++;
            break;
        }
    }
    for (; r >= 0; r--) {//尾部出现过'T'
        if (chas[r] == 'T') {
            r--;
            break;
        }
    }
    //当[l] ='T'和 [r] ='M' 同时成立时,while循环结束
    while (!((chas[l] == 'T') && (chas[r] == 'M'))) {
        if (chas[l] != 'T') l++;
        if (chas[r] != 'M') r--;
    }
    System.out.println(T.substring(l + 1, r));
}

小团的选调计划

  • 用一个vis的bool数组,来标记从小到大编号的业务骨干是否已经被安排了,被安排了,表示这个区域当前的业务骨干j不可以去了
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int n = Integer.parseInt(br.readLine().trim());
            boolean[] vis = new boolean[n + 1];
            for (int i = 0; i < n; i++) {
                String[] choices = br.readLine().split(" ");
                for (int j = 0; j < choices.length; j++) {
                    int cur = Integer.parseInt(choices[j]);
                    if (!vis[cur]) {
                        vis[cur] = true;
                        System.out.printf("%d ", cur);
                        break;
                    }
                }
            }
        }
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值