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-有成绩的学生人数 M-要查询的学生人数
学号 成绩(共N行)
学号(查询)
输出
最好的排名 科目(优先级A > C > M > E)。若该学生没有成绩则输出"N/A"。注意是输一个学号输出一次!!!
思路
结构体存学生信息(如下)
typedef struct Student{
string id; //学号
int grade[4];//分别按顺序为C M E A
int rand[4];//各科的排名
int mark;//排名最优的学科下标
}Student;
首先输入各个学生的成绩,计算均分,初始排名都置为1。
遍历计算排名。将每个学生都与其他学生比一遍,比他成绩低,排名就+1。这种方法不用特意去处理成绩并列问题
然后对每个学生,按优先级找出最高排名的学科下标
输入学号后首先遍历结构体数组查找学号相等的,有则输出,flag置1,遍历一遍还没有则输出“N/A”
问题
1.一开始以为是一次性输入了所有要查询的学号再输出结果,一直0分。后来看了下别人的代码才发现,应该是输入一个查询学号立即输出查询结果……
2.成绩并列问题。当成绩并列时,类似1 2 2 3 是不行的,要分别存成 1 2 2 4 。由于我的算法在计算排名时是将当前的分数与其他所有人的都比一遍,如果有两个比他成绩好的,自然排名会+2。
代码
#include<stdio.h>
#include<iostream>
#include <stdlib.h>
#include <string>
#include<iomanip>
using namespace std;
typedef struct Student{
string id; //学号
int grade[4];//分别按顺序为C M E A
int rand[4];//各科的排名
int mark;//排名最优的学科下标
}Student;
int main(){
char str[4] = {'C','M','E','A'};
string s_id[2000];
int n,m;
int i,j;
cin>>n>>m;
Student s[2000];
for(i = 0;i<n;i++){
cin>>s[i].id>>s[i].grade[0]>>s[i].grade[1]>>s[i].grade[2];
s[i].grade[3] = (s[i].grade[0]+s[i].grade[1]+s[i].grade[2])/3;
s[i].rand[0] = 1;
s[i].rand[1] = 1;
s[i].rand[2] = 1;
s[i].rand[3] = 1;
}
// for(i = 0;i<m;i++){
// cin>>s_id[i];
// }
// cout<<endl<<"----------------------------"<<endl;
for(i = 0;i<n;i++){
for(j = 0;j<n;j++){
if(i!=j){
if(s[i].grade[0]<s[j].grade[0]) s[i].rand[0]++;
if(s[i].grade[1]<s[j].grade[1]) s[i].rand[1]++;
if(s[i].grade[2]<s[j].grade[2]) s[i].rand[2]++;
if(s[i].grade[3]<s[j].grade[3]) s[i].rand[3]++;
}
}
// cout<<s[i].rand[0]<<" "<<s[i].rand[1]<<" "<<s[i].rand[2]<<" "<<s[i].rand[3]<<endl;
}
// cout<<endl<<"----------------------------"<<endl;
int t;
for(i = 0;i<n;i++){
t = s[i].rand[3];
s[i].mark = 3;
for(j = 0;j<3;j++){
if(t>s[i].rand[j]){
s[i].mark = j;
t = s[i].rand[j];
}
}
// cout<<s[i].rand[mark]<<" "<<str[mark]<<endl;
}
int flag;
for(i = 0;i<m;i++){
string s_id;
cin>>s_id;
flag = 0;
for(j = 0;j<n;j++){
if(s_id==s[j].id){
cout<<s[j].rand[s[j].mark]<<" "<<str[s[j].mark]<<endl;
flag = 1;
}
}
if(!flag) cout<<"N/A"<<endl;
}
return 0;
}