蓝桥杯--算法提高 成绩排序(java)

资源限制
时间限制: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
————————————————————————————————————————————
学号从1开始的,题目也没说。。。

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	static int n;
	static Stu[] stus;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		stus = new Stu[n];
		for(int i = 0;i < n;i++) {
			stus[i] = new Stu(sc.nextInt(), sc.nextInt(), sc.nextInt(), i);
		}
		sc.close();
		
		Arrays.sort(stus, new Comparator<Stu>() {
			@Override
			public int compare(Stu o1, Stu o2) {
				if(o1.math != o2.math) {
					return o2.math - o1.math;
				}else if(o1.english != o2.english) {
					return o2.english - o1.english;
				}else if(o1.chinese != o2.chinese) {
					return o2.chinese - o1.chinese;
				}else {
					return o1.id - o2.id;
				}
			}
		});
		for(int i = 0;i < n;i++) {
			System.out.printf("%d %d %d %d\n", stus[i].math, stus[i].english, stus[i].chinese, stus[i].id + 1);
		}
	}
}
class Stu{
	int math;
	int chinese;
	int english;
	int id;
	public Stu(int math, int english, int chinese, int id) {
		this.math = math;
		this.chinese = chinese;
		this.english = english;
		this.id = id;
	}
}
发布了177 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/QinLaoDeMaChu/article/details/105643534