思路:把德分和才分存入数组中,作为Map中的value,ID作为key。自定义考生类person,将person的属性设为Map.Entry<key,value>,随后重写compareTo()方法,伴随一个动态数组存储person类型的对象,最后Collections.sort()进行排序。
也是意料之中的超时了,各位如果有AC的java代码也可以分享下!
代码如下:
import java.io.*;
import java.util.*;
/*
考生类 包含键值对属性 key为准考证号 value为德分与才分构成的数组
按照题意重写compareTo()方法
*/
class person implements Comparable{
Map.Entry<Integer,int[]> info;
public person(Map.Entry<Integer,int[]> info){
this.info=info;
}
//求总分方法
public static int sum(int[] array){
int sum=0;
for(int temp:array){
sum+=temp;
}
return sum;
}
@Override
public int compareTo(Object o){
person score=(person) o;
int sum1=sum(this.info.getValue());
int sum2=sum(score.info.getValue());
int virtue1=this.info.getValue()[0];
int virtue2=score.info.getValue()[0];
int ID1=this.info.getKey();
int ID2=score.info.getKey();
//先按总分排
if(sum1!=sum2){
return -(sum1-sum2);
}
//总分并列德分排
else if(virtue1!=virtue2){
return -(virtue1-virtue2);
}
//德分并列ID排
else{
return (ID1-ID2);
}
}
}
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
in.nextToken();
int N=(int)in.nval;
in.nextToken();
int L=(int)in.nval;
in.nextToken();
int M=(int)in.nval;
/*
先创建一个Map对象,再伴随一个ArrayList对象用来存储Map中的键值对并排序
*/
Map<Integer,int[]> first=new HashMap<>();
ArrayList<person> firstList=new ArrayList<>();
Map<Integer,int[]> second=new HashMap<>();
ArrayList<person> secondList=new ArrayList<>();
Map<Integer,int[]> third=new HashMap<>();
ArrayList<person> thirdList=new ArrayList<>();
Map<Integer,int[]> forth=new HashMap<>();
ArrayList<person> forthList=new ArrayList<>();
for(int i=1;i<=N;i++){
in.nextToken();
int ID=(int)in.nval;
in.nextToken();
int virtue=(int)in.nval;
in.nextToken();
int ability=(int)in.nval;
int[] array={virtue,ability};
/*
按照题意放入对应的Map对象中
*/
if(virtue>=M&&ability>=M){
first.put(ID,array);
}
else if(ability<M&&virtue>=M&&ability>=L){
second.put(ID,array);
}
else if(ability>=L&&virtue>=L&&virtue>=ability){
third.put(ID,array);
}
else if(ability>=L&&virtue>=L){
forth.put(ID,array);
}
}
/*
entrySet()方法返回键值对对象,再作为person类存入对应的ArrayList中
*/
for(Map.Entry<Integer,int[]> info:first.entrySet()){
person p=new person(info);
firstList.add(p);
}
for(Map.Entry<Integer,int[]> info:second.entrySet()){
person p=new person(info);
secondList.add(p);
}
for(Map.Entry<Integer,int[]> info:third.entrySet()){
person p=new person(info);
thirdList.add(p);
}
for(Map.Entry<Integer,int[]> info:forth.entrySet()){
person p=new person(info);
forthList.add(p);
}
/*
对四个集合分别排序
*/
Collections.sort(firstList);
Collections.sort(secondList);
Collections.sort(thirdList);
Collections.sort(forthList);
//先打录取总人数
out.println(firstList.size()+secondList.size()+ thirdList.size()+forthList.size());
/*
再按题目要求打印个档次学生的信息
*/
for(person p:firstList){
out.println(p.info.getKey()+" "+p.info.getValue()[0]+" "+p.info.getValue()[1]);
}
for(person p:secondList){
out.println(p.info.getKey()+" "+p.info.getValue()[0]+" "+p.info.getValue()[1]);
}
for(person p:thirdList){
out.println(p.info.getKey()+" "+p.info.getValue()[0]+" "+p.info.getValue()[1]);
}
for(person p:forthList){
out.println(p.info.getKey()+" "+p.info.getValue()[0]+" "+p.info.getValue()[1]);
}
out.flush();
}
}