1 题目
2 代码
2.1 第一版
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class B1015{
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int M = Integer.parseInt(input[0]);
int L = Integer.parseInt(input[1]);
int H = Integer.parseInt(input[2]);
ArrayList<ExamStudent> oneArr = new ArrayList<>();
ArrayList<ExamStudent> twoArr = new ArrayList<>();
ArrayList<ExamStudent> thrArr = new ArrayList<>();
ArrayList<ExamStudent> fouArr = new ArrayList<>();
for (int i = 0; i < M; i++) {
String[] str = br.readLine().split(" ");
String id = str[0];
int D = Integer.parseInt(str[1]);
int C = Integer.parseInt(str[2]);
if (D < L || C < L)
continue;
ExamStudent student = new ExamStudent(id, D, C);
if (D >= H && C >= H) {
oneArr.add(student);
} else if (D >= H && C < H) {
twoArr.add(student);
} else if (D < H && C < H && D >= C) {
thrArr.add(student);
} else {
fouArr.add(student);
}
}
Collections.sort(oneArr, new Comparator<ExamStudent>() {
@Override
public int compare(ExamStudent o1, ExamStudent o2) {
if (o1.getdValue() + o1.getcValue() != o2.getdValue() + o2.getcValue())
return o2.getdValue() + o2.getcValue() - o1.getdValue() - o1.getcValue();
else if (o1.getdValue() != o2.getdValue())
return o2.getdValue() - o1.getdValue();
return o1.getId().compareTo(o2.getId());
}
});
Collections.sort(twoArr, new Comparator<ExamStudent>() {
@Override
public int compare(ExamStudent o1, ExamStudent o2) {
if (o1.getdValue() + o1.getcValue() != o2.getdValue() + o2.getcValue())
return o2.getdValue() + o2.getcValue() - o1.getdValue() - o1.getcValue();
else if (o1.getdValue() != o2.getdValue())
return o2.getdValue() - o1.getdValue();
return o1.getId().compareTo(o2.getId());
}
});
Collections.sort(thrArr, new Comparator<ExamStudent>() {
@Override
public int compare(ExamStudent o1, ExamStudent o2) {
if (o1.getdValue() + o1.getcValue() != o2.getdValue() + o2.getcValue())
return o2.getdValue() + o2.getcValue() - o1.getdValue() - o1.getcValue();
else if (o1.getdValue() != o2.getdValue())
return o2.getdValue() - o1.getdValue();
return o1.getId().compareTo(o2.getId());
}
});
Collections.sort(fouArr, new Comparator<ExamStudent>() {
@Override
public int compare(ExamStudent o1, ExamStudent o2) {
if (o1.getdValue() + o1.getcValue() != o2.getdValue() + o2.getcValue())
return o2.getdValue() + o2.getcValue() - o1.getdValue() - o1.getcValue();
else if (o1.getdValue() != o2.getdValue())
return o2.getdValue() - o1.getdValue();
return o1.getId().compareTo(o2.getId());
}
});
System.out.println(oneArr.size() + twoArr.size() + thrArr.size() + fouArr.size());
for (ExamStudent student : oneArr) {
System.out.println(student.getId() + " " + student.getdValue() + " " + student.getcValue());
}
for (ExamStudent student : twoArr) {
System.out.println(student.getId() + " " + student.getdValue() + " " + student.getcValue());
}
for (ExamStudent student : thrArr) {
System.out.println(student.getId() + " " + student.getdValue() + " " + student.getcValue());
}
for (ExamStudent student : fouArr) {
System.out.println(student.getId() + " " + student.getdValue() + " " + student.getcValue());
}
}
}
class ExamStudent {
private String id;
private int dValue;
private int cValue;
public ExamStudent(String id, int dValue, int cValue) {
this.id = id;
this.dValue = dValue;
this.cValue = cValue;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getdValue() {
return dValue;
}
public void setdValue(int dValue) {
this.dValue = dValue;
}
public int getcValue() {
return cValue;
}
public void setcValue(int cValue) {
this.cValue = cValue;
}
}
2.2 第二版
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class B1015 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int M = Integer.parseInt(input[0]);
int L = Integer.parseInt(input[1]);
int H = Integer.parseInt(input[2]);
ArrayList<ExamStudent> arr = new ArrayList<>();
for (int i = 0; i < M; i++) {
String[] str = br.readLine().split(" ");
String id = str[0];
int D = Integer.parseInt(str[1]);
int C = Integer.parseInt(str[2]);
if (D < L || C < L)
continue;
if (D >= H && C >= H) {
arr.add(new ExamStudent(id, D, C, 1));
} else if (D >= H && C < H) {
arr.add(new ExamStudent(id, D, C, 2));
} else if (D < H && C < H && D >= C) {
arr.add(new ExamStudent(id, D, C, 3));
} else {
arr.add(new ExamStudent(id, D, C, 4));
}
}
Collections.sort(arr, new Comparator<ExamStudent>() {
@Override
public int compare(ExamStudent o1, ExamStudent o2) {
if (o2.getLevel() != o1.getLevel())
return o1.getLevel() - o2.getLevel();
else if (o1.getdValue() + o1.getcValue() != o2.getdValue() + o2.getcValue())
return o2.getdValue() + o2.getcValue() - o1.getdValue() - o1.getcValue();
else if (o1.getdValue() != o2.getdValue())
return o2.getdValue() - o1.getdValue();
return o1.getId().compareTo(o2.getId());
}
});
System.out.println(arr.size());
for (ExamStudent student : arr) {
System.out.println(student.getId() + " " + student.getdValue() + " " + student.getcValue());
}
}
}
class ExamStudent {
private String id;
private int dValue;
private int cValue;
private int level;
public ExamStudent(String id, int dValue, int cValue, int level) {
this.id = id;
this.dValue = dValue;
this.cValue = cValue;
this.level = level;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getdValue() {
return dValue;
}
public void setdValue(int dValue) {
this.dValue = dValue;
}
public int getcValue() {
return cValue;
}
public void setcValue(int cValue) {
this.cValue = cValue;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
3 要点
(1)首先想到的是利用4个数组存储不同类别的考生,但是内容较为庞杂,重复的代码较多,借鉴网上的经验,在每个考生处标明类别,可以在一个数组中进行排序。
(2)但是在Java中进行的排序,测试点2、3和4会运行超时,如果有更好的方法,望告知。