生日宴(利用数列排序)
import java.io.IOException;
import java.util.*;
import java.util.Collections;
import java.util.Comparator;
public class Main {
static class People{
String name;
int bir;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
ArrayList<People> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
People p = new People();
p.name = sc.next();
p.bir = sc.nextInt();
list.add(p);
}
Collections.sort(list, new Comparator<People>() {
@Override
public int compare(People o1, People o2) {
return o1.bir > o2.bir?1:-1;
}
});
while(m-- > 0){
int k = sc.nextInt();
int s = sc.nextInt();
int count = 0;
for (int i = 0; i < n; i++) {
int a = list.get(i).bir;
if(a % 10000 == s){
count++;
}
if(count == k){
System.out.println(list.get(i).name);
break;
}
}
}
}
}
##看来是需要再复习一次类的排序了,不过这里需要用到的是新方法。开始创建一个list数列,然后创建People类,把名字和生日放进去。在主类中实例化,然后加入到list中,再用collections方法比较生日的大小(这里是从大到小排序),之后输入k,s。从大到小排序的话直接比较list中和输入的是否相同,count+1计数,当到k的时候就输出名字。