7-9 学生成绩排序 (20分)

7-9 学生成绩排序 (20分)

给出一组学生名单(姓名和成绩),请输出按成绩从大到小排序后的结果(名次和姓名)。成绩相同的人拥有相同的名次,且按姓名的字典序从小到大排列。题目保证学生姓名没有重复。
输入格式:

第一行给出不大于10000的整数N。 接下来N行,每行给出学生姓名和成绩,以空格分隔。学生姓名不超过20个字符。
输出格式:

共输出N行,每行包含对应的学生排名和姓名,以空格分隔。
输入样例:

7
KongDezhen 94
FuTaotao 93
HuYu 94
XuJiecen 94
WuSuqi 96
ChenDa 93
HuNianbo 95

输出样例:

1 WuSuqi
2 HuNianbo
3 HuYu
3 KongDezhen
3 XuJiecen
6 ChenDa
6 FuTaotao

最后一个点总是不能通过

在这里插入图片描述


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Linked l = new Linked();
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for (int i = 0; i < n; i++) {
			String name = sc.next();
			int grade = sc.nextInt();
			char c = name.charAt(0);
			Node node = new Node(name, grade, c);
			l.add(node);
		}
		sc.close();
		l.show();

	}
}

class Linked {
	Node head = new Node("", 0, ' ');

	// 添加
	public void add(Node node) {
		// 看看头的下一个有没有元素
		Node temp = head;
		int node_grade = node.grade;
		int node_name_firstC = node.c;
		while (true) {
			if (temp.next == null) {
				temp.next = node;
				return;
			}
			// 1:按照分数高低
			if (node_grade > temp.next.grade) {
				node.next = temp.next;
				temp.next = node;
				return;
				// 分数相同看字母按照顺序排序
			} else if (node_grade == temp.next.grade) {
				if (node_name_firstC < temp.next.c) {
					node.next = temp.next;
					temp.next = node;
					return;
				}
			}
			temp = temp.next;
		}

	}

	// 获取长度
	public int getLength() {
		int x = 0;
		Node temp = head.next;
		while (temp != null) {
			x++;
			temp = temp.next;
		}
		return x;
	}

	// 打印
	public void show() {
		Node temp = head.next;
		int i = 1;// 并列名次
		int t = 0;
		int rank = 0;
		int index = 1;
		int cishu = 1;
		while (temp != null) {
			if (index != 1) {
				if (cishu != getLength() - 1) {
					if (temp.grade == t) {
						rank++;
						System.out.println(i + " " + temp.name);
					} else {
						t = temp.grade;
						rank++;
						i = rank;
						System.out.println(i + " " + temp.name);
					}
					cishu++;
				} else {
					if (temp.grade == t) {
						rank++;
						System.out.print(i + " " + temp.name);
					} else {
						t = temp.grade;
						rank++;
						i = rank;
						System.out.print(i + " " + temp.name);
					}
				}
			} else {

				index = 0;
				System.out.println(i + " " + temp.name);
				t = temp.grade;
				rank++;

			}
			temp = temp.next;
		}
	}
}

class Node {
	Node next;
	String name;
	int grade;
	int c;

	public Node(String name, int grade, char c) {
		this.name = name;
		this.grade = grade;
		this.c = c;
	}
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
的学生及其学号。 回答: (1) 使用随机数模拟学生成绩。 ```python import numpy as np # 随机生成10名学生的成绩 np.random.seed(0) scores = np.random.randint(50, 101, size=(10, 5)) # 学生学号 students = np.array([100, 101, 102, 103, 104, 105, 106, 107, 108, 109]) print("10名学生的成绩为:\n", scores) ``` (2) 查询学号为105的学生的英语成绩。 ```python english_score = scores[5, 2] print("学号为105的学生的英语成绩为:", english_score) ``` (3) 查询学号为100,102,105,109的四位学生的语文、数学和英语成绩。 ```python selected_scores = scores[[0, 2, 5, 9]][:, :3] print("学号为100,102,105,109的四位学生的语文、数学和英语成绩为:\n", selected_scores) ``` (4) 查询大于或者等于90成绩和相应学生的学号。 ```python high_scores = np.where(scores >= 90) for i in range(len(high_scores[0])): print("学号为{}的学生在{}科目中获得{}".format(students[high_scores[0][i]], ['语文', '数学', '英语', '生物', '历史'][high_scores[1][i]], scores[high_scores[0][i], high_scores[1][i]])) ``` (5) 按各门课程的成绩排序。 ```python sorted_scores = np.sort(scores, axis=0)[::-1] print("按各门课程的成绩排序:\n", sorted_scores) ``` (6) 按每名学生的成绩排序。 ```python sorted_scores = np.sort(scores, axis=1)[::-1] print("按每名学生的成绩排序:\n", sorted_scores) ``` (7) 计算每门课程的平均、最高和最低。 ```python print("每门课程的平均、最高和最低为:") for i, subject in enumerate(['语文', '数学', '英语', '生物', '历史']): subject_scores = scores[:, i] print("{}:平均{},最高{},最低{}".format(subject, np.mean(subject_scores), np.max(subject_scores), np.min(subject_scores))) ``` (8) 计算每名学生的最高和最低。 ```python print("每名学生的最高和最低为:") for i, student in enumerate(students): student_scores = scores[i] print("学号为{}的学生:最高{},最低{}".format(student, np.max(student_scores), np.min(student_scores))) ``` (9) 查询最低及相应学生的学号和课程。 ```python min_score = np.min(scores) min_indices = np.argwhere(scores == min_score) for i in range(len(min_indices)): print("学号为{}的学生在{}科目中获得最低{}".format(students[min_indices[i][0]], ['语文', '数学', '英语', '生物', '历史'][min_indices[i][1]], min_score)) ``` (10) 查询最高及相应的学生学号和课程。 ```python max_score = np.max(scores) max_indices = np.argwhere(scores == max_score) for i in range(len(max_indices)): print("学号为{}的学生在{}科目中获得最高{}".format(students[max_indices[i][0]], ['语文', '数学', '英语', '生物', '历史'][max_indices[i][1]], max_score)) ``` (11) 语文、数学、英语、生物、历史这5门课程在总中的占比别为25%、25%、20%、15%、15%。如果总为100,则计算每名学生的总成绩。 ```python weights = np.array([0.25, 0.25, 0.2, 0.15, 0.15]) total_scores = np.dot(scores, weights) / np.sum(weights) * 100 for i, student in enumerate(students): print("学号为{}的学生总成绩为{}".format(student, total_scores[i])) ``` (12) 查询最高的3个总的学生及其学号。 ```python top_k = 3 # 查询前3名 idx = np.argsort(total_scores)[::-1][:top_k] for i in range(top_k): print("第{}名学生:学号为{},总成绩为{}".format(i+1, students[idx[i]], total_scores[idx[i]])) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理想艺术!马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值