算法提高 成绩排序

Lan
Lan
2020-03-28 / 0 评论 / 788 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年06月30日,已超过992天没有更新,若内容或图片失效,请留言反馈。
资源限制
时间限制:1.0s   内存限制:256.0MB
问题描述
  给出n个学生的成绩,将这些学生按成绩排序,
  排序规则,优先考虑数学成绩,高的在前;数学相同,英语高的在前;数学英语都相同,语文高的在前;三门都相同,学号小的在前
输入格式
  第一行一个正整数n,表示学生人数
  接下来n行每行3个0~100的整数,第i行表示学号为i的学生的数学、英语、语文成绩
输出格式
  输出n行,每行表示一个学生的数学成绩、英语成绩、语文成绩、学号
  按排序后的顺序输出
样例输入
2
1 2 3
2 3 4
样例输出
2 3 4 2
1 2 3 1
数据规模和约定
  n≤100
import java.util.*;

public class 成绩排序 {
	public static class student {
		public int math;
		public int engilsh;
		public int chinese;
		public int id;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		student[] student = new student[n];
		for (int i = 0; i < n; i++) {
			student[i] = new student();
			student[i].math = sc.nextInt();
			student[i].engilsh = sc.nextInt();
			student[i].chinese = sc.nextInt();
			student[i].id = i + 1;
		}
		for (int i = 0; i < student.length; i++) {
			for (int j = i; j < student.length; j++) {
				if (student[i].math < student[j].math) {
					student temp = student[i];
					student[i] = student[j];
					student[j] = temp;
				}else if (student[i].math==student[j].math) {
					if (student[i].engilsh<student[j].engilsh) {
						student temp = student[i];
						student[i] = student[j];
						student[j] = temp;
					}else if (student[i].engilsh==student[j].engilsh) {
						if (student[i].chinese<student[j].chinese) {
							student temp = student[i];
							student[i] = student[j];
							student[j] = temp;
						}else if (student[i].chinese==student[j].chinese) {
							if (student[i].id>student[j].id) {
								student temp = student[i];
								student[i] = student[j];
								student[j] = temp;
							}
						}
					}
				}
			}
		}
		for (int i = 0; i < student.length; i++) {
			System.out.println(student[i].math + " " + student[i].engilsh + " "
					+ student[i].chinese + " " + student[i].id);
		}
	}

}


0

评论 (0)

取消