前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >试题 算法提高 成绩排序2

试题 算法提高 成绩排序2

作者头像
SingYi
发布2022-07-13 18:44:13
发布2022-07-13 18:44:13
22700
代码可运行
举报
文章被收录于专栏:Lan小站Lan小站
运行总次数:0
代码可运行

资源限制

时间限制: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

代码语言:javascript
代码运行次数:0
复制
import java.util.Scanner;

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

	/**
	 * @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;
			student[i].all = student[i].math+student[i].engilsh+student[i].chinese;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n-i-1; j++) {
				if (student[j].all<student[j+1].all) {
					student temp = student[j];
					student[j] = student[j+1];
					student[j+1] = temp;
				}else if (student[j].all==student[j+1].all) {
					if (student[j].math<student[j+1].math) {
						student temp = student[j];
						student[j] = student[j+1];
						student[j+1] = temp;
					}else if (student[j].math == student[j+1].math) {
						if (student[j].engilsh<student[j+1].engilsh) {
							student temp = student[j];
							student[j] = student[j+1];
							student[j+1] = temp;
						}else if (student[j].engilsh==student[j+1].engilsh) {
							if (student[j].id>student[j+1].id) {
								student temp = student[j];
								student[j] = student[j+1];
								student[j+1] = temp;
							}
						}
					}
				}
			}
		}
		for (int i = 0; i < student.length; i++) {
			System.out.println(student[i].math + " " + student[i].engilsh + " "
					+ student[i].chinese + " " + student[i].id);
		}
	}

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档