猿辅导2020校招编程题-2

记录校招编程题

第一题

package jan3;

import java.util.*;

/**
 * 输入:
 * 第一行:两个数,学生报数总个数n,和允许的最大重复次数 m,以空格分隔
 * 第二行:n个整数,表示学生所有报数数列,以空格分隔,范围是-2147483648~2147483647
 * 输出:
 * 只有一行,去除超出m次的报数数字后的报数数列,该数列不改变原报数顺序,数列以空格分隔
 */
public class P1 {

    private int[] solve(int stu_num, int rep_num, int[] arr) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int i : arr) map.put(i, map.getOrDefault(i, 0) + 1);
        List<Integer> res = new ArrayList<>();
        for(int i : arr) if(map.get(i) <= rep_num) res.add(i);
        return res.stream().mapToInt(Integer::intValue).toArray();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line1 = sc.nextLine();
        String line2 = sc.nextLine();
        String[] nums = line1.split(" ");
        String[] arr_s = line2.split(" ");
        int stu_num = Integer.parseInt(nums[0]);
        int rep_num = Integer.parseInt(nums[1]);
        int[] arr = new int[stu_num];
        for(int i = 0; i < stu_num; i++) arr[i] = Integer.parseInt(arr_s[i]);
        P1 p = new P1();
        int[] res = p.solve(stu_num, rep_num, arr);
        for(int i : res) System.out.print(i + " ");
    }
}

第二题

题意不清。。

第三题

package jan3;
import java.util.*;

/**
 * 猿辅导课堂上老师提供了一些角色,学生可以从中选择一个自己喜欢的角色扮演,每3个不同的角色就可以组成一个小组,进行分组对话。
 * 当老师点击开始分组对话按钮的时候,服务器会为已经选择自己角色的同学分配对话小组,请问最多能组成多少个对话小组?
 * 优先队列
 */
public class P3{
    public static void main(String[] args){
        int C, T, i, j;
        Scanner input = new Scanner(System.in);
        C = input.nextInt();
        int[][] P = new int[C][];
        for(i = 0; i < C; i++){
            T = input.nextInt();
            P[i] = new int[T];
            for(j = 0; j < T; j++) P[i][j] = input.nextInt();
        }
        for(i = 0; i < C; i++) System.out.println(Solution(P[i]));
    }

    private static int Solution(int[] P){
        int first, second, third, ans;
        PriorityQueue<Integer> pq;

        ans = 0;
        pq = new PriorityQueue<>((a, b) -> b - a);
        for(int p : P) if(p > 0) pq.offer(p);
        while(pq.size() > 2){
            first = pq.poll();
            second = pq.poll();
            third = pq.poll();
            if(--first > 0) pq.offer(first);
            if(--second > 0) pq.offer(second);
            if(--third > 0) pq.offer(third);
            ans++;
        }
        return ans;
    }
}

猜你喜欢

转载自www.cnblogs.com/xlsryj/p/12145694.html