成绩排序 290 java 题解

25 篇文章 1 订阅
10 篇文章 0 订阅

问题描述

  给出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

解题思路:

纯排序,直接封装成类,调用Comparable接口,重写compareTo()方法

java代码:

import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		List<Temp290> list = new ArrayList<>();
		for(int i = 1; i <= n;i++) {
			String[] split = br.readLine().split(" ");
			int math = Integer.parseInt(split[0]);
			int english = Integer.parseInt(split[1]);
			int chinese = Integer.parseInt(split[2]);
			list.add(new Temp290(math, english, chinese, i));
		}
		Collections.sort(list);
		for(Temp290 data : list) {
			System.out.println(data);
		}
	}
}
class Temp290 implements Comparable<Temp290>{
	int math;
	int english;
	int chinese;
	int num;
	
	public Temp290(int math, int english, int chinese, int num) {
		this.math = math;
		this.english = english;
		this.chinese = chinese;
		this.num = num;
	}

	@Override
	public String toString() {
		return math + " " + english + " " + chinese + " " + num;
	}

	@Override
	public int compareTo(Temp290 o) {
		if(this.math != o.math) {
			return o.math - this.math;
		}else if(this.english != o.english) {
			return o.english - this.english;
		}else if(this.chinese != o.chinese) {
			return o.chinese - this.chinese;
		}else {
			return this.num - o.num;
		}
	}
}

 

提交截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值