欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

PAT乙级1015德才论java测试点3、4超时测试点2压线

时间:2023-06-07

 

思路:把德分和才分存入数组中,作为Map中的value,ID作为key。自定义考生类person,将person的属性设为Map.Entry,随后重写compareTo()方法,伴随一个动态数组存储person类型的对象,最后Collections.sort()进行排序。

也是意料之中的超时了,各位如果有AC的java代码也可以分享下!

代码如下:

import java.io.*;import java.util.*;class person implements Comparable{ Map.Entry info; public person(Map.Entry 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 first=new HashMap<>(); ArrayList firstList=new ArrayList<>(); Map second=new HashMap<>(); ArrayList secondList=new ArrayList<>(); Map third=new HashMap<>(); ArrayList thirdList=new ArrayList<>(); Map forth=new HashMap<>(); ArrayList 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}; if(virtue>=M&&ability>=M){ first.put(ID,array); } else if(ability=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); } } for(Map.Entry info:first.entrySet()){ person p=new person(info); firstList.add(p); } for(Map.Entry info:second.entrySet()){ person p=new person(info); secondList.add(p); } for(Map.Entry info:third.entrySet()){ person p=new person(info); thirdList.add(p); } for(Map.Entry 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(); }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。