1012. The Best Rank (25)

本文介绍了一个学生排名系统的设计与实现,该系统通过比较学生的C语言编程、数学和英语成绩及平均分来确定最佳排名,并提供了C语言、C++和Java三种实现方案。

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

1012. The Best Rank (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 Algebra), 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

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

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

我的代码(C语言):

#include<stdio.h>
#include<string.h>
struct student
{
	char id[8];
	int a,c,m,e;
}s[2001];
int n,m,i,j,k=0,x[2001],y[2001];
int a[2001],b[2001],c[2001],d[2001];
void bubble(int a[],int n)
{
	int i,j,t;
	for(i=n-1;i>=0;i--)
	{
		for(j=0;j<i;j++)
		{
			if(a[j]<a[j+1])
			{
				t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
			}
		}
	}
}
int main()
{
	char num[8];
	scanf("%d%d%*c",&n,&m);
	for(i=0;i<n;i++)
	{
		scanf("%s%d%d%d%*c",s[i].id,&s[i].c,&s[i].m,&s[i].e);
		s[i].a=(s[i].c+s[i].m+s[i].e)/3;
		a[k]=s[i].c,b[k]=s[i].m,c[k]=s[i].e,d[k]=s[i].a;
		k++;
	}
	bubble(a,k);
	bubble(b,k);
	bubble(c,k);
	bubble(d,k);
	for(i=0;i<n;i++)
	{
		int rank=0,min=2010;
		for(j=0;j<k;j++)
		{
			rank++;
			if(s[i].e==c[j])
			{
				if(rank<=min)
				{
					min=rank;
					x[i]='E'-'0';
				}
				break;
			}
		}
		rank=0;
		for(j=0;j<k;j++)
		{
			rank++;
			if(s[i].m==b[j])
			{
				if(rank<=min)
				{
					min=rank;
					x[i]='M'-'0';
				}
				break;
			}
		}
		rank=0;
		for(j=0;j<k;j++)
		{
			rank++;
			if(s[i].c==a[j])
			{
				if(rank<=min)
				{
					min=rank;
					x[i]='C'-'0';
				}
				break;
			}
		}
		rank=0;
		for(j=0;j<k;j++)
		{
			rank++;
			if(s[i].a==d[j])
			{
				if(rank<=min)
				{
					min=rank;
					x[i]='A'-'0';
				}
				break;
			}
		}
		y[i]=min;
	}
	while(m--)
	{
		scanf("%s",num);
		int flag=0;
		for(i=0;i<n;i++)
		{
			if(strcmp(num,s[i].id)==0)
			{
				flag=1;
				break;
			}
		}
		if(flag==0) puts("N/A");
		else printf("%d %c\n",y[i],x[i]+'0');
	}
	return 0;
}

提交结果:



我的代码(C++):

#include<vector>
#include<map>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
struct student 
{
	string id;
	int c,m,e,a;
}s[2001];
map<string,int>p,ppp;
map<string,char>pp;
int main()
{
	int n,m,i;
	cin>>n>>m;
	vector<int>cc,mm,ee,aa;
	for(i=0;i<n;i++)
	{
		cin>>s[i].id>>s[i].c>>s[i].m>>s[i].e;
		p[s[i].id]++;
		s[i].a=(s[i].c+s[i].m+s[i].e)/3;
		cc.push_back(s[i].c);
		mm.push_back(s[i].m);
		ee.push_back(s[i].e);
		aa.push_back(s[i].a);
	}
	sort(cc.begin(),cc.end());
	reverse(cc.begin(),cc.end());
	sort(mm.begin(),mm.end());
	reverse(mm.begin(),mm.end());
	sort(ee.begin(),ee.end());
	reverse(ee.begin(),ee.end());
	sort(aa.begin(),aa.end());
	reverse(aa.begin(),aa.end());
	for(i=0;i<n;i++)
	{
		vector<int>::iterator it;
		int j=0,k=2010;
		for(it=ee.begin();it!=ee.end();it++)
		{
			j++;
			if(s[i].e==*it)
			{
				if(j<=k)
				{
					k=j;
					pp[s[i].id]='E';
				}
				break;
			}
		}
		j=0;
		for(it=mm.begin();it!=mm.end();it++)
		{
			j++;
			if(s[i].m==*it)
			{
				if(j<=k)
				{
					k=j;
					pp[s[i].id]='M';
				}
				break;
			}
		}
		j=0;
		for(it=cc.begin();it!=cc.end();it++)
		{
			j++;
			if(s[i].c==*it)
			{
				if(j<=k)
				{
					k=j;
					pp[s[i].id]='C';
				}
				break;
			}
		}
		j=0;
		for(it=aa.begin();it!=aa.end();it++)
		{
			j++;
			if(s[i].a==*it)
			{
				if(j<=k)
				{
					k=j;
					pp[s[i].id]='A';
				}
				break;
			}
		}
		ppp[s[i].id]=k;
	}
	string x;
	while(m--)
	{
		cin>>x;
		if(p[x]==0) puts("N/A");
		else cout<<ppp[x]<<" "<<pp[x]<<endl;
	}
	return 0;
}

提交结果:



我的代码(Java):

(修改前:运行严重超时)

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;	
class student
{
	String id;
	int c,m,e,a;
}
public class Main {
	public static void main(String[] args) {
		HashMap<String,Integer>p=new HashMap<String,Integer>(),
		pp=new HashMap<String,Integer>(),ppp=new HashMap<String,Integer>();
		Scanner In=new Scanner(System.in);
		int n=In.nextInt(),m=In.nextInt();
		ArrayList<Integer>cc=new ArrayList<Integer>(),mm=new ArrayList<Integer>(),
		ee=new ArrayList<Integer>(),aa=new ArrayList<Integer>();
		student s[]=new student[2001];
		for(int i=0;i<n;i++)
		{
			s[i]=new student();
			s[i].id=In.next();
			s[i].c=In.nextInt();
			s[i].m=In.nextInt();
			s[i].e=In.nextInt();
			s[i].a=(s[i].c+s[i].m+s[i].e)/3;
			p.put(s[i].id, 1);
			cc.add(s[i].c);
			mm.add(s[i].m);
			ee.add(s[i].e);
			aa.add(s[i].a);
		}
		Collections.sort(cc);
		Collections.reverse(cc);
		Collections.sort(mm);
		Collections.reverse(mm);
		Collections.sort(ee);
		Collections.reverse(ee);
		Collections.sort(aa);
		Collections.reverse(aa);
		for(int i=0;i<n;i++)
		{
			int j=0,k=2010;
			for(Iterator it1=ee.iterator();it1.hasNext();)
			{
				j++;
				if(s[i].e==(int)it1.next())
				{
					if(j<=k)
					{
						k=j;	
						pp.put(s[i].id, 'E'-'0');
					}
					break;
				}
			}
			j=0;
			for(Iterator it2=mm.iterator();it2.hasNext();)
			{
				j++;
				if(s[i].m==(int)it2.next())
				{
					if(j<=k)
					{
						k=j;
						pp.put(s[i].id,'M'-'0');
					}
					break;
				}
			}
			j=0;
			for(Iterator it3=cc.iterator();it3.hasNext();)
			{
				j++;
				if(s[i].c==(int)it3.next())
				{
					if(j<=k)
					{
						k=j;
						pp.put(s[i].id,'C'-'0');
					}
					break;
				}
			}
			j=0;
			for(Iterator it4=aa.iterator();it4.hasNext();)
			{
				j++;
				if(s[i].a==(int)it4.next())
				{
					if(j<=k)
					{
						k=j;
						pp.put(s[i].id, 'A'-'0');
					}
					break;
				}
			}
			ppp.put(s[i].id,k);
		}
		for(int i=0;i<m;i++)
		{
			String num=In.next();
			if(p.get(num)==null) System.out.printf("N/A\n");
			else System.out.printf("%d %c\n", ppp.get(num),pp.get(num)+'0');
		}
	}
}

我的代码(Java):

(修改后:在牛客网上可以通过)

import java.util.Scanner;
class student
{
	String id;
	int c,m,e,a;
}
public class Main {
	public static void bubble(int a[],int n)
	{
		int i,j,t;
		for(i=n-1;i>=0;i--)
		{
			for(j=0;j<i;j++)
			{
				if(a[j]<a[j+1])
				{
					t=a[j];
					a[j]=a[j+1];
					a[j+1]=t;
				}
			}
		}
	}
	public static void main(String[] args) {
		Scanner In=new Scanner(System.in);
		int n,m,i,j,k=0,x[]=new int[2001],y[]=new int[2001];
		int a[]=new int[2001],b[]=new int[2001];
		int c[]=new int[2001],d[]=new int[2001];
		student s[]=new student[2001];
		n=In.nextInt();
		m=In.nextInt();
		for(i=0;i<n;i++)
		{
			s[i]=new student();
			s[i].id=In.next();
			s[i].c=In.nextInt();
			s[i].m=In.nextInt();
			s[i].e=In.nextInt();
			s[i].a=(s[i].c+s[i].m+s[i].e)/3;
			a[k]=s[i].c;
			b[k]=s[i].m;
			c[k]=s[i].e;
			d[k]=s[i].a;
			k++;
		}
		bubble(a,k);
		bubble(b,k);
		bubble(c,k);
		bubble(d,k);
		for(i=0;i<n;i++)
		{
			int rank=0,min=2010;
			for(j=0;j<k;j++)
			{
				rank++;
				if(s[i].e==c[j])
				{
					if(rank<=min)
					{
						min=rank;
					    x[i]='E'-'0';
					}
					break;
				}
			}
			rank=0;
			for(j=0;j<k;j++)
			{
				rank++;
				if(s[i].m==b[j])
				{
					if(rank<=min)
					{
						min=rank;
					    x[i]='M'-'0';
					}
					break;
				}
			}
			rank=0;
			for(j=0;j<k;j++)
			{
				rank++;
				if(s[i].c==a[j])
				{
					if(rank<=min)
					{
						min=rank;
					    x[i]='C'-'0';
					}
					break;
				}
			}
			rank=0;
			for(j=0;j<k;j++)
			{
				rank++;
				if(s[i].a==d[j])
				{
					if(rank<=min)
					{
						min=rank;
						x[i]='A'-'0';
					}
					break;
				}
			}
			y[i]=min;
		}
		for(i=0;i<m;i++)
		{
			String num=In.next();
			int flag=0;
			for(j=0;j<n;j++)
			{
				if(num.equals(s[j].id))
				{
					flag=1;
					break;
				}
			}
			if(flag==0) System.out.printf("N/A\n");
			else System.out.printf("%d %c\n",y[j],x[j]+'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)|的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值