PAT乙级算法题“德才论”用java写的解法总是内存超限(eclipse中测试过,结果应该是没有问题的),希望有大神帮忙改进下一下。内存限制 32768 KB

teddy169 2017-07-12 10:18:41
题目描述
宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之

小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”



现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入描述:
输入第1行给出3个正整数,分别为:N(<=105),即考生总数;L(>=60),为录取最低分数线,即德分和才分均不低于L的考生才有资格

被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到

但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于H,但是德分不低于才分的考生属于“才德兼

亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线L的考生也按总分排序,但排在第三类考生之后。


随后N行,每行给出一位考生的信息,包括:准考证号、德分、才分,其中准考证号为8位整数,德才分为区间[0, 100]内的整数。数字间以空格分隔。


输出描述:
输出第1行首先给出达到最低分数线的考生人数M,随后M行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人

总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入例子:
14 60 80

10000001 64 90

10000002 90 60

10000011 85 80

10000003 85 80

10000004 80 85

10000005 82 77

10000006 83 76

10000007 90 78

10000008 75 79

10000009 59 90

10000010 88 45

10000012 80 100

10000013 90 99

10000014 66 60

输出例子:
12

10000013 90 99

10000012 80 100

10000003 85 80

10000011 85 80

10000004 80 85

10000007 90 78

10000006 83 76

10000005 82 77

10000002 90 60

10000014 66 60

10000008 75 79

10000001 64 90

以上是题目。
我的解决代码如下:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class Main {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
int n = scan.nextInt(); //得到学生成绩个数
int m = 0;
int lowLine = scan.nextInt(); //获得低分限
int highLine = scan.nextInt(); //获得高分限
//对象数组stuAll用来获取所有学生信息
Student[] stuAll = new Student[n];
//slist用来保存达标学生信息
List<Student> slist = new ArrayList<Student>();
//firstlist用来保存第一等学生信息
List<Student> firstlist = new ArrayList<Student>();
//firstlist用来保存第二等学生信息
List<Student> secondlist = new ArrayList<Student>();
//firstlist用来保存第三等学生信息
List<Student> thirdlist = new ArrayList<Student>();
//firstlist用来保存第四等学生信息
List<Student> lastlist = new ArrayList<Student>();
for(int i = 0; i < n ; i++){
stuAll[i] = new Student();
//获取输入的每行的学生信息
stuAll[i].id = scan.nextLong();
stuAll[i].dScore = scan.nextInt();
stuAll[i].cScore = scan.nextInt();
stuAll[i].sumScore = stuAll[i].dScore + stuAll[i].cScore;
//达标的加入slist
if(stuAll[i].dScore >= lowLine && stuAll[i].cScore >= lowLine){
m++;
slist.add(stuAll[i]);
stuAll[i] = null;
}
}
stuAll = null;
System.out.println(m);


for(int i = 0; i < m; i++){
/**
* 4类达标考生分类填入列表
*/
if(slist.get(i).dScore >= highLine && slist.get(i).cScore >= highLine){
firstlist.add(slist.get(i));//德才兼备
}else if(slist.get(i).dScore >= highLine && slist.get(i).cScore < highLine){
secondlist.add(slist.get(i));//德胜才
}else if(slist.get(i).dScore < highLine && slist.get(i).cScore < highLine &&
slist.get(i).dScore >= slist.get(i).cScore){
thirdlist.add(slist.get(i));//德才兼亡尚有德胜才
}else{
lastlist.add(slist.get(i));
}
}
slist = null;
/**
* 排序和输出
*/
/*ScoreComparator comp = new ScoreComparator();*/
Collections.sort(firstlist);
for(int i = 0 ; i < firstlist.size(); i++){
System.out.println(firstlist.get(i));
}
firstlist = null;

Collections.sort(secondlist);
for(int i = 0 ; i < secondlist.size(); i++){
System.out.println(secondlist.get(i));
}
secondlist = null;

Collections.sort(thirdlist);
for(int i = 0 ; i < thirdlist.size(); i++){
System.out.println(thirdlist.get(i));
}
thirdlist = null;

Collections.sort(lastlist);
for(int i = 0 ; i < lastlist.size(); i++){
System.out.println(lastlist.get(i));
}
lastlist = null ;
}
}

//学生类,重写compareTo和stringTo方法
class Student implements Comparable<Student>{
public long id = 1;
public int dScore = 60;
public int cScore = 60;
public int sumScore = 100;
public int compareTo(Student s){
if (this.sumScore > s.sumScore){
return -1;
}else if (this.sumScore == s.sumScore){
if(this.dScore > s.dScore){
return -1;
}else if(this.dScore == s.dScore){
if(this.id > s.id){
return 1;
}else{
return -1;
}
}
}
return 1; //这里返回值1和-1还是有区别的,返回的应是上面遗漏的情况
}
public String toString(){
return this.id + " " + this.dScore + " " + this.cScore;
}
}
...全文
225 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,626

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧