A1012 The Best Rank (25 分)

本文介绍了一个学生排名系统的设计与实现,该系统针对计算机科学专业一年级学生的成绩进行排名,包括编程、数学和英语三门课程,以及平均成绩。系统采用多种排序方法,确保公平评估学生表现,并强调最佳排名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID C M E A
310101 98 85 88 90
310102 70 95 88 84
310103 82 87 94 88
310104 91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:
Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output Specification:
For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input:
5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output:
1 C
1 M
1 E
1 A
3 A
N/A

很尴尬,刚开始做这道题时,思维上出现了很大的漏洞
有几点要注意的:

  1. 注意审题,题目说的是排名的比较,而不是分数比较,所以在读取数据时取最大值行不通(对每个人)
  2. 按题目中例子,平均值要四舍五入,考虑round()函数
  3. 关键点在于把所有学生分别在四种排序方法下的rank值保存起来
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct stu
{
	int id;//每位学生的编号
	int score[4];//分别存放A、C、M、E四项的成绩
	int rank[4];//存放每种排序方法下的rank值
};
stu a[2010];//用于存放所有学生成绩信息
char ch[4] = {'A','C','M','E'};
int now;//表示当前排序方式
bool cmp(stu a,stu b)
{
	return a.score[now]>b.score[now];
}
int main()
{
	int n, m , MAX=-1;
	stu tmp;
	scanf("%d%d",&n,&m);
	for (int i=0;i<n;i++)
	{
		MAX = -1;
		scanf("%d%d%d%d",&tmp.id,&tmp.score[1],&tmp.score[2],&tmp.score[3]);
		tmp.score[0] = round((tmp.score[1]+ tmp.score[2] + tmp.score[3])/3.0);
		a[i] = tmp;
	}

	for (now=0;now<4;now++)//保存四种排序方法下的rank值
	{
		sort(a,a+n,cmp);
		a[0].rank[now] = 1;
		for (int i=1;i<n;i++)
		{
			if (a[i].score[now]==a[i-1].score[now])
			{
				a[i].rank[now] = a[i - 1].rank[now];
			}
			else
			{
				a[i].rank[now] = i + 1;
			}
		}
	}
	int name,flag=0,MIN=2010,MIN_id;
	for (int i=0;i<m;i++)//对每一个查询编号进行处理
	{
		MIN = 2010;
		flag = 0;
		scanf("%d",&name);
		for (int j=0;j<n;j++)//与所有的学生编号进行比较
		{
			if (name==a[j].id)//输入的编号存在
			{
				flag = 1;
				for (int k=0;k<4;k++)//对该编号所有科目的rank值进行遍历,找到排名最前的科目及其rank值
				{
					if (MIN>a[j].rank[k])
					{
						MIN = a[j].rank[k];
						MIN_id = k;
					}
				}
				printf("%d %c\n", MIN, ch[MIN_id]);
				break;
			}
		}
		if (flag==0)//输入的编号不存在
		{
			printf("N/A\n");
		}
	}
	return 0;
}
下面是Python中使用Numpy库解决该问题的示例代码: ```python import numpy as np # 读取数据集A.cav A = np.loadtxt('A.cav') # 计算A的SVD U, S, VT = np.linalg.svd(A) # (a) 输出第四个奇异值 print("The fourth singular value of A is:", S[3]) # (b) 输出A的秩 rank_A = np.linalg.matrix_rank(A) print("The rank of A is:", rank_A) # (c) 计算特征解 eigvals, eigvecs = np.linalg.eig(np.dot(A.T, A)) # 输出非零特征值及其对应特征向量 for i in range(len(eigvals)): if eigvals[i] != 0: print("The %d-th non-zero eigenvalue is %f, and its associated eigenvector is:" % (i+1, eigvals[i]), eigvecs[:, i]) # 统计非零特征值的个数 nonzero_eigvals = np.count_nonzero(eigvals) print("There are %d non-zero eigenvalues." % nonzero_eigvals) # (d) 计算A_k k = 3 Ak = np.dot(np.dot(U[:, :k], np.diag(S[:k])), VT[:k, :]) print("A_k for k=3 is:\n", Ak) # (e) 计算A-Ak A_Ak = A - Ak print("A - A_k is:\n", A_Ak) # (f) PCA降维 m = 4 mean_A = np.mean(A, axis=0) A_centered = A - mean_A C = np.dot(A_centered.T, A_centered) eigvals_pca, eigvecs_pca = np.linalg.eig(C) idx = eigvals_pca.argsort()[::-1] eigvals_pca = eigvals_pca[idx] eigvecs_pca = eigvecs_pca[:, idx] F = np.dot(A_centered, eigvecs_pca[:, :3]) print("The best 3-dimensional subspace F is:\n", F) # (g) 计算|A-Ak|和|A-π(A)| norm_A_Ak = np.linalg.norm(A_Ak) print("|A - A_k| is:", norm_A_Ak) norm_A_pca = np.linalg.norm(A - np.dot(F, eigvecs_pca[:, :3].T) + mean_A) print("|A - π(A)| is:", norm_A_pca) ``` 首先,读取数据集A.cav,然后使用`np.linalg.svd()`函数进行SVD解,并将解后的三个矩阵别赋值给变量U、S、VT。根据问题要求,依次输出第四个奇异值、A的秩、特征解的结果、非零特征值的个数、A_k、A-A_k、PCA降维后的最佳3维子空间F,以及|A-A_k|和|A-π(A)|的值。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值