51,408
社区成员
发帖
与我相关
我的任务
分享
/// 人数多时,最好做成Map<分数,List<学生>>
/// 因为分数才几百个,同一个分数的人可以就有几千个
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
*
* @author ba
*/
public class px {
public static void main(String[] args) {
List<School> schools = new ArrayList();
List<Student> students = new ArrayList();
//填装数据
load(schools, students);
//学校,学生排序
java.util.Collections.sort(students);
java.util.Collections.sort(schools);
//对比
Comparator(schools, students);
//显示结果
printStudet(students);
printSchool(schools);
}
static void load(List<School> schools, List<Student> students) {
schools.add(new School("技校", 700));
schools.add(new School("新东方", 780));
schools.add(new School("清华", 500));
schools.add(new School("北大", 350));
schools.add(new School("师大", 400));
for (int i = 0; i < 10; i++) {
schools.add(new School(getNameRand() + "大学", 200 + Math.abs(rand.nextInt() % 610)));
}
for (int i = 0; i < 100; i++) {
students.add(new Student(getNameRand(), Math.abs(rand.nextInt() % 781)));
}
}
private static void Comparator(List<School> schools, List<Student> students) {
for (int i = 0, im = schools.size(); i < im; i++) {
School school = schools.get(i);
for (int j = students.size() - 1; j > -1; j--) {
Student student = students.get(j);
//上线
if (student.getScore() >= school.getScoreLine()) {
student.getSchools().add(school);
school.getStudents().add(student);
} else {
break;
}
}
}
}
private static void printStudet(List<Student> students) {
}
private static void printSchool(List<School> schools) {
if (schools != null && !schools.isEmpty()) {
for (School school : schools) {
List<Student> students = school.getStudents();
if (students == null || students.isEmpty()) {
System.out.println("" + school.getSchoolName() + " 分数线为:" + school.getScoreLine() + ",没人上线.");
} else {
System.out.println(school.getSchoolName() + " 分数线为:" + school.getScoreLine() + ",有" + students.size() + "人上线.");
System.out.println("上线名单:");
for (Student student : students) {
System.out.println(" "+student.getStudentName() + "\t" + student.getScore());
}
}
System.out.println("");
}
}
}
/**得到随机名*/
static String getNameRand() {
String n = "";
int c = rand.nextInt(10) + 1;
while (c-- > 0) {
n += getCharRand();
}
return n;
}
/**得到随机汉字*/
static char getCharRand() {
return (char) (19968 + rand.nextInt(40870 - 19968));
}
/**随机种子*/
static Random rand = new Random();
}
/**学校信息*/
class School implements Comparable<School> {
/**学校名*/
private String schoolName;
/**分数线*/
private Double scoreLine = 0.0;
/**上线的学生*/
private List<Student> students = new ArrayList();
public School() {
}
public School(String name, double sc) {
schoolName = name;
scoreLine = sc;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
public Double getScoreLine() {
return scoreLine;
}
public void setScoreLine(Double scoreLine) {
this.scoreLine = scoreLine;
}
public int compareTo(School o) {
return (int) (this.scoreLine - o.scoreLine);
}
}
/**学生信息*/
class Student implements Comparable<Student> {
/**学生名*/
private String studentName;
/**分数*/
private Double score = 0.0;
/**上线的学校*/
private List<School> schools = new ArrayList();
public Student() {
}
public Student(String name, double sc) {
studentName = name;
score = sc;
}
public List<School> getSchools() {
return schools;
}
public void setSchools(List<School> schools) {
this.schools = schools;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int compareTo(Student o) {
return (int) (this.score - o.score);
}
}