淘汰分数
题意:划定一个分数线,大于这个分数的选手晋级,其他的选手就淘汰
要求:n名选手,要求找出一个最低分数m,按分数划分成晋级和淘汰两波人,并且这两波人人数都在[x,y]区间内
思路:要求按成绩划分,肯定得先将n名选手成绩放入数组并按由低到高排序, 遍历数组将成绩放入栈中,并判断当前栈的大小是否满足[x,y] 当前数组剩余元素个数是否满足[x,y],如果都满足,直接结束循环,否则继续放如栈中。 最后判断跳出循环是因为break还是循环结束(栈中元素个数),如果是因为break,那么直接弹出栈顶元素,得到答案;要不没有找到合适的分数m,返回-1;
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] array = new int[3];
for(int i = 0 ; i < 3; i++) {
array[i] = sc.nextInt();
}
int n = array[0];
int x = array[1];
int y = array[2];
int[] solary = new int[n];
for(int i = 0 ; i < n; i++) {
solary[i] = sc.nextInt();
}
Arrays.sort(solary);
Stack<Integer> stack = new Stack<>();
for(int i = 0;i < n; i++) {
stack.push(solary[i]);
if((stack.size() >= x && stack.size() <= y) && (n-i-1 >= x && n-i-1 <= y)){
break;
}
}
if(stack.size() != n) {
System.out.println(stack.pop());
}else{
System.out.println(-1);
}
}
}