信息学院年终评定奖学金,需要对整个年级的学生按照平均分数进行排名。 要求:根据输入的学号和平均成绩,按照平均成绩降序输出学号,如果平均成绩相同,按照输入的顺序输出。

信息学院年终评定奖学金,需要对整个年级的学生按照平均分数进行排名。

要求:根据输入的学号和平均成绩,按照平均成绩降序输出学号,如果平均成绩相同,按照输入的顺序输出。

 

 

//信息学院年终评定奖学金,需要对整个年级的学生按照平均分数进行排名。
//要求:根据输入的学号和平均成绩,按照平均成绩降序输出学号,如果平均成绩相同,按照输入的顺序输出。

学生类
package three;

public class Student implements Comparable {
	private long id;
	private double grade;

	public Student(long id, double grade) {
		this.id = id;
		this.grade = grade;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public double getGrade() {
		return grade;
	}

	public void setGrade(double grade) {
		this.grade = grade;
	}

	public int compareTo(Object o) {
		Student stu = (Student) o;
		int result = grade < stu.grade ? 1 
				: (grade == stu.grade ? 0 : -1);
		return result;

	}

}

 

 

//主类


package three;

import java.util.*;

public class Main {
	public static void main(String[] args) {
		TreeSet stu = new TreeSet();
		Scanner cin = new Scanner(System.in);
		long id;
		double grade;
		id = cin.nextLong();
		grade = cin.nextDouble();
		while (id != 0) {
			Student s = new Student(id, grade);
			stu.add(s);
			id = cin.nextLong();
			grade = cin.nextDouble();
		}
		System.out.println("排序之后");
		Iterator stu1 = stu.iterator();
		while (stu1.hasNext()) {
			Student ss = stu1.next();
			System.out.println(ss.getId() + "   " + ss.getGrade());
		}
	}
}

前面这种方法会自动把平均数相同的给去掉

下面这种方法不会去重

package three;

public class Student {
	private long id;
	private double grade;

	public Student(long id, double grade) {
		this.id = id;
		this.grade = grade;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public double getGrade() {
		return grade;
	}

	public void setGrade(double grade) {
		this.grade = grade;
	}


}
package three;

import java.util.*;

public class Main {
	public static void main(String[] args) {
		List stu = new ArrayList();
		Scanner cin = new Scanner(System.in);
		long id;
		double grade;
		id = cin.nextLong();
		grade = cin.nextDouble();
		while (id != 0) {
			Student s = new Student(id, grade);
			stu.add(s);
			id = cin.nextLong();
			grade = cin.nextDouble();
		}
		Collections.sort(stu,new Comparator(){
			public int compare(Student s,Student s1) {
				if (s.getGrade()

 

你可能感兴趣的:(JAVA)