题意:
抓住一点,按照A—>C—>M—>E的顺序取每门成绩的最高排名,因为每个学生的成绩是鼓励式的,所以允许有并列。如果只有A,B两个学生,且A和B的Average都是90,但是A的C语言成绩比B的C语言成绩高,B仍然输出 1 A。
解题思路:
用4个ArrayList<Student>存储学生排名,分别按照A,C,M,E升序排列。同时用一个Map来存储<Student.id, Student.rank[]>键值对,在代码里面对应<String, int[]>,前面代表学生ID,后面顺序存储学生的A,C,M,E四门成绩的排名。在处理排名数组的时候注意可以并列,用 lastIndex和 lastGrade两个变量可以解决这个问题,而不是简单的对应遍历下标 i(坑)。最后输出的时候遍历对应学生ID的rank[]数组,输出最小的就行。
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Set;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
String nm[] = rd.readLine().split(" ");
int N = Integer.parseInt(nm[0]);
int M = Integer.parseInt(nm[1]);
ArrayList<Student> aList = new ArrayList<>();
ArrayList<Student> cList = new ArrayList<>();
ArrayList<Student> mList = new ArrayList<>();
ArrayList<Student> eList = new ArrayList<>();
Map<String, int[]> map = new HashMap<>();
for(int i=0; i<N; i++) {
String str[] = rd.readLine().split(" ");
String id = str[0];
int c = Integer.parseInt(str[1]);
int m = Integer.parseInt(str[2]);
int e = Integer.parseInt(str[3]);
Student stu = new Student(id, c, m, e);
aList.add(stu);
cList.add(stu);
mList.add(stu);
eList.add(stu);
map.put(id, new int[4]);
}
Collections.sort(aList, new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
return o2.avg - o1.avg ;
}
});
// System.out.println("\n-------------");
// for(Student e: aList)
// System.out.print(e.avg + " ");
// System.out.println("\n-------------");
int lastIndex = 0;
int lastGrade = aList.get(lastIndex).avg;
for(int i=0; i<aList.size(); i++) {
int temp = aList.get(i).avg;
if(lastGrade != temp) {
lastIndex = i;
}
map.get(aList.get(i).id)[0] = lastIndex+1;
lastGrade = temp;
}
Collections.sort(cList, new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
return o2.c - o1.c;
}
});
lastIndex = 0;
lastGrade = cList.get(lastIndex).c;
for(int i=0; i<cList.size(); i++) {
int temp = cList.get(i).c;
if(lastGrade != temp)
lastIndex = i;
map.get(cList.get(i).id)[1] = lastIndex+1;
lastGrade = temp;
}
Collections.sort(mList, new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
return o2.m - o1.m;
}
});
lastIndex = 0;
lastGrade = mList.get(lastIndex).m;
for(int i=0; i<mList.size(); i++) {
int temp = mList.get(i).m;
if(lastGrade != temp)
lastIndex = i;
map.get(mList.get(i).id)[2] = lastIndex+1;
lastGrade = temp;
}
Collections.sort(eList, new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
return o2.e - o1.e;
}
});
lastIndex = 0;
lastGrade = eList.get(lastIndex).e;
for(int i=0; i<eList.size(); i++) {
int temp = eList.get(i).e;
if(lastGrade != temp)
lastIndex = i;
map.get(eList.get(i).id)[3] = lastIndex+1;
lastGrade = temp;
}
// Set<Entry<String, int[]>> entrySet = map.entrySet();
// for(Entry<String, int[]> e: entrySet) {
// System.out.print(e.getKey());
// int temp[] = e.getValue();
// for(int i=0; i<temp.length; i++)
// System.out.print(" " + temp[i]);
// System.out.println();
// }
//
for(int i=0; i<M; i++) {
String check = rd.readLine();
if(!map.containsKey(check))
System.out.println("N/A");
else {
int rank[] = map.get(check);
int index = 0;
int bestRank = rank[0];
for(int j=1; j<rank.length; j++) {
if(rank[j] < bestRank) {
bestRank = rank[j];
index = j;
}
}
if(index == 0) System.out.printf("%d A\n", bestRank);
if(index == 1) System.out.printf("%d C\n", bestRank);
if(index == 2) System.out.printf("%d M\n", bestRank);
if(index == 3) System.out.printf("%d E\n", bestRank);
}
}
}
static class Student{
String id;
int c;
int m;
int e;
int avg;
public Student(String id, int c, int m, int e){
this.id = id;
this.c = c;
this.m = m;
this.e = e;
this.avg = (c+m+e) / 3;
}
}
}
写在后面:
这题捣鼓了快2个小时,觉得思路是没问题的,就是java太慢了=.=,3和4一直不通过。但是C和C++又太菜,改天再回来补一补吧。。。。。。