顺丰 9.7 java笔试编程题第二题 构造试卷
考完就突然会写了,真是麻了
题目:有n种题型,每种题型的数量各异,出一份试卷需要由m道题型各不相同的题构成
输入:第一个数是n,第二个数是m,下面的一行是每种题型的数量
5 3
8 5 4 7 2
输出
8
思路:
整体思路就是将每种题型根据数量排序,然后每次将前m多的题型扣掉第m多的题型的数量
然后再排序再扣除,直至剩余题型不足m种,循环结束
import java.util.*;
public class Main {
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(sc.nextInt());
}
// System.out.println("输入完毕");
int res = 0;
while (true) {
list.sort((a, b) -> {
return b - a;
});
System.out.println(list.toString());
// 这是从大到小,第m多的题型的数量,它决定了这一次出的套数
int min = list.get(m - 1);
// 遍历前m种
for (int i = 0; i < m; i++) {
// 该题型的数量
int amout = list.get(i);
// 扣除min
list.set(i, amout - min);
}
// 将用完的题型踢出
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i) == 0) {
list.remove(i);
}
}
// 这样就出好了min份试卷
res += min;
// 如果省下的题型不足m,那么就无法再出卷了
if (list.size() < m) {
break;
}
}
System.out.println(res);
}
} #顺丰笔试#