1.题目要求输出学生最好的排名,如果多个科目的排名相同,则优先考虑是平均分A,再依次为C,M,E。
2.通过重写不同的cmp函数,使用algorithm.h里面的sort进行排序
3.如果分数相同,则排名相同,例如90,80,80,80,70,则排名为1,2,2,2,5。这个题目没有明确说明,但是实际上要这样排名。
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 language=”++”]
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
struct student{
int c, m, e, rank;
int a;
string course, ID;
int tmpRank;//用来存在不同状态下的排名,主要是更新并列排名
student() :c(0), m(0), e(0), a(0), rank(9999999), tmpRank(9999999), course(""), ID(""){};
};
bool cmpC(const student&a, const student&b)
{
if (a.c > b.c) return true;
else return false;
}
bool cmpM(const student&a, const student&b)
{
if (a.m > b.m) return true;
else return false;
}
bool cmpE(const student&a, const student&b)
{
if (a.e > b.e) return true;
else return false;
}
bool cmpA(const student&a, const student&b)
{
if (a.a > b.a) return true;
else return false;
}
int main(void) {
int n, m;
cin >> n >> m;
vector<student> stu(n);
for (int i = 0; i < n; i++)
{
cin >> stu[i].ID >> stu[i].c >> stu[i].m >> stu[i].e;
stu[i].a = (stu[i].c + stu[i].m + stu[i].e);
stu[i].course = "";
stu[i].rank = 9999999;
}
//比较平均分
sort(stu.begin(), stu.end(), cmpA);
for (int i = 0; i < n; i++)
{
if (i == 0)
stu[i].tmpRank = 0;
else if (stu[i].a == stu[i – 1].a)
stu[i].tmpRank = stu[i – 1].tmpRank;//如果分数相同,则排名相同
else
stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4
if (stu[i].rank > stu[i].tmpRank + 1)
{
stu[i].rank = stu[i].tmpRank + 1;
stu[i].course = "A";
}
}
//比较C语言
sort(stu.begin(), stu.end(), cmpC);
for (int i = 0; i < n; i++)
{
if (i == 0)
stu[i].tmpRank = 0;
else if (stu[i].c == stu[i – 1].c)
stu[i].tmpRank = stu[i – 1].tmpRank;//如果分数相同,则排名相同
else
stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4
if (stu[i].rank > stu[i].tmpRank + 1)
{
stu[i].rank = stu[i].tmpRank + 1;
stu[i].course = "C";
}
}
//比较Mathematics
sort(stu.begin(), stu.end(), cmpM);
for (int i = 0; i < n; i++)
{
if (i == 0)
stu[i].tmpRank = 0;
else if (stu[i].m == stu[i – 1].m)
stu[i].tmpRank = stu[i – 1].tmpRank;//如果分数相同,则排名相同
else
stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4
if (stu[i].rank > stu[i].tmpRank + 1)
{
stu[i].rank = stu[i].tmpRank + 1;
stu[i].course = "M";
}
}
//比较English
sort(stu.begin(), stu.end(), cmpE);
for (int i = 0; i < n; i++)
{
if (i == 0)
stu[i].tmpRank = 0;
else if (stu[i].e == stu[i – 1].e)
stu[i].tmpRank = stu[i – 1].tmpRank;//如果分数相同,则排名相同
else
stu[i].tmpRank = i;//分数不同,则是i,例如 100 90 90 80 ,排名应该为1,2,2,4
if (stu[i].rank > stu[i].tmpRank + 1)
{
stu[i].rank = stu[i].tmpRank + 1;
stu[i].course = "E";
}
}
map<string, student> ma;
for (int i = 0; i < n; i++)
{
ma[stu[i].ID] = stu[i];
}
for (int i = 0; i < m; i++)
{
string tmp;
cin >> tmp;
if (ma.find(tmp) == ma.end())
cout << "N/A" << endl;
else
cout << ma[tmp].rank << " " << ma[tmp].course << endl;
}
return 0;
}
[/c]