*甲级PAT 1012 The Best Rank

1012 The Best Rank (25)(25 分)

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

题目要求: 

给出N个学生的学号以及C语言、数学、英语三门课的成绩表单。

再M个学生的学号,若在上述成绩表单中则找出他C语言、数学、英语和三门均分(各科成绩范围0-100)中排名最高的一项并输出。若有多门相同,则按照均分>C语言>数学>英语的顺序输出排名最高的一项。若不在成绩表单中则输出N/A

解题思路:

首先构建一个结构体用于存储学生学号、C语言、数学、英语、均分信息。

然后用一个数组scorelist[4][101]记录各项排行情况。4代表了四门学科,101代表从0分到100分。scorelist[0][1]表示C语言0分的人数。scorelist[3][100]表示均分100的人数。这样后面对输入成绩表单中所有学生的各项成绩进行遍历,找到各项对应的排行,用数组Rank[maxsize][4]进行存储。Rank[i][0]表示第i位学生的C语言排名。

对于输入的学生学号,首先要判断是否在成绩表单内,若不在,则输出N/A;若在则判断他四项排名哪个最高。这里要写比较。初始最高排名min和最高为第几项index设均分为值,即  min = Rank[i][3];index = 3;。然后若有排名比均值高,更新min和index。最后输出最高的。

注意:

1.命名变量不能用rank 会报错reference to ‘rank’ is ambiguous。原因是自定义的rank变量与库中重名,修改为Rank即可。之前就一直编译错误

2.这里的排名是 1 1 3 4 5 而非 1 1 2 3 4。一定要注意这个。 所以数组scorelist不再只是一个标识位,而是当前分值所拥有的人数。之前只是用0和1表示这个分数有人而没有记录人数。测试点3一直错误。

完整代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
#define maxsize 2001

typedef struct Student{
	string id;
	int C;
	int M;
	int E;
	int A;
}Student;

Student stus[maxsize];
int scorelist[4][101]; //存储所有的分数 
int Rank[maxsize][4]; 
int M,N;

int findid(string s){
	int j;
	for(j=0;j<N;j++){
		if(stus[j].id == s){
			return j;
		}
	}
	return -1;	
}

void countrank(int i){
	int min = Rank[i][3];
	int index = 3;
	for(int j=0;j<3;j++){
		if(Rank[i][j]<min){
			index = j;
			min = Rank[i][j];
		}
	}
	if(index == 0){
		cout<<Rank[i][0]<<" "<<'C'<<endl; 
	}else if(index == 1){
		cout<<Rank[i][1]<<" "<<'M'<<endl; 
	}else if(index == 2){
		cout<<Rank[i][2]<<" "<<'E'<<endl; 
	}else if(index == 3){
		cout<<Rank[i][3]<<" "<<'A'<<endl; 
	}
}

int main(){
	int i,j,aver,index;
	string searchid;
	cin>>N>>M;
	for(i=0;i<4;i++){
		memset(scorelist[i],0,sizeof(scorelist[i]));
	}
	for(i=0;i<N;i++){
		cin>>stus[i].id>>stus[i].C>>stus[i].M>>stus[i].E;
		stus[i].A = (stus[i].C+stus[i].M+stus[i].E)/3;
		scorelist[0][stus[i].C] += 1;
		scorelist[1][stus[i].M] += 1;
		scorelist[2][stus[i].E] += 1;
		scorelist[3][stus[i].A] += 1;
	}
	for(i=0;i<N;i++){
		int j,r=1;
		for(j=100;j>=0;j--){
			if(scorelist[0][j] > 0){
				if(j==stus[i].C){
					Rank[i][0] = r;
				}else{
					r+=scorelist[0][j];
				}
			}
		}
		r=1;
		for(j=100;j>=0;j--){
			if(scorelist[1][j] > 0){
				if(j==stus[i].M){
					Rank[i][1] = r;
				}else{
					r+=scorelist[1][j];
				}
			}
		}
		r=1;
		for(j=100;j>=0;j--){
			if(scorelist[2][j] > 0){
				if(j==stus[i].E){
					Rank[i][2] = r;
				}else{
					r+=scorelist[2][j];
				}
			}
		}
		r=1;
		for(j=100;j>=0;j--){
			if(scorelist[3][j] > 0){
				if(j==stus[i].A){
					Rank[i][3] = r;
				}else{
					r+=scorelist[3][j];
				}
			}
		}		
	}
	for(i=0;i<M;i++){
		cin>>searchid;
		index = findid(searchid);
		if(index==-1){
			cout<<"N/A"<<endl;
		}else{
			countrank(index);
		}
	}
	
	return 0;
}

代码优化

2019.2.7

之前代码中重复的循环都可以用数组来替代,用一个flag来分别表示不同的科目就可以了

而且上面的findid找到对应Id所存储的下标的方法,实际上是一种浪费时间。由于这里的id是6位数字,完全可以用一个hash,用exist数组的下标表示id,对应的内容表示存储该学生信息的下标。大大节省了时间。但是由于这里学生的存储是从0开始,exist可以存储下标+1,这样,既可以判断这个id的学生存不存在(exist[i]>0)还可以获得对应的下标(exist[i]-1)

由于成绩平均分可能为小数,需要四舍五入,所以将除以3之后的结果再加上0.5,强制转化为int就是四舍五入的结果。

代码简洁了很多

#include<bits/stdc++.h>
using namespace std;

struct Stu{
	int id,best;
	int score[4],rank[4]; //A C M E
}stu[2001];

int exist[1000000],flag=-1;
bool com(Stu stu1,Stu stu2) {
	return stu1.score[flag] > stu2.score[flag];	
}

int main(){
	int n,m,i,j,min,id,temp,best;
	char c[5]={'A','C','M','E'};
	memset(exist,0,sizeof(exist));
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++){
		scanf("%d %d %d %d",&stu[i].id,&stu[i].score[1],&stu[i].score[2],&stu[i].score[3]);
		for(j=1;j<=3;j++){
			stu[i].score[0]+=stu[i].score[j]; 
		}
		stu[i].score[0]=stu[i].score[0]/3.0 + 0.5;
	}
	for(i=0;i<=3;i++){
		flag=i;
		sort(stu,stu+n,com);
		stu[0].rank[i]=1;
		for(j=1;j<n;j++){
			if(stu[j].score[i] == stu[j-1].score[i]) stu[j].rank[i]=stu[j-1].rank[i];
			else stu[j].rank[i]=j+1;
		}
	}
	for(i=0;i<n;i++){
		exist[stu[i].id]=i+1;
		min=stu[i].rank[0];
		stu[i].best=0;
		for(j=1;j<=3;j++){
			if(stu[i].rank[j] < min){
				min=stu[i].rank[j];	
				stu[i].best=j;
			}
		}
	}
	for(i=0;i<m;i++){
		scanf("%d",&id);
		temp=exist[id];
		best=stu[temp-1].best;
		if(temp){
			printf("%d %c\n",stu[temp-1].rank[best],c[best]);
		}else printf("N/A\n");
	}
	return 0;
}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值