PAT甲级1012. The Best Rank

题意:

为了评估我们第一年的CS专业学生的表现,我们只考虑他们的三个课程的成绩:C - C编程语言,M - 数学(微积分或线性代数)和E - 英语。同时,我们鼓励学生强调自己的最优秀队伍 - 也就是说,

在三个课程和平均成绩的四个职级中,我们打印每个学生的最佳排名。

例如,C,M,E和A - 4名学生的成绩如下:

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

那么所有学生的最佳排名都是第一,因为第一名在C编程语言方面做得最好,而第二名则是数学第二名,英文第三名,平均第三名。

输入

每个输入文件包含一个测试用例。每个案例以包含2个数字N和M(<= 2000)的行开始,

分别是学生人数和学生人数。然后按N行,每个包含一个6位数字的学生ID,其后是C,M和E顺序的该学生的三个整数等级(在[0,100]范围内)。然后在那里是M行,每行包含学生ID

输出

对于每个M学生,以一行打印他/她的最佳排名,以及由空格分隔的相应排名的符号。

排名方法的优先级排序为A> C> M> E。因此,如果学生获得相同最佳排名有两种或更多种方式,则输出优先级最高的排名。

如果学生不在评分清单上,只需输出“N / A”即可。

思路:

麻烦的地方就是排序了,我写的好丑。题意中排名方法的优先级排序为A> C> M> E 可以通过构造一个count[4]数组的方法来实现。只要当前下标下的数比后面的数都要小于或这等于就可以成立。

ac代码:

C++

// pat1012.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<unordered_map> using namespace std; struct stu {
string id;
int c;
int m;
int e;
int a;
char bestsub;
int best;
}; void getbest(vector<stu>& map)
{
char ans[4] = { 'A','C','M','E' };
int len = map.size();
for (int i = 0; i < len; i++)
{
//c = m = e = a = 1;
int count[4] = { 1,1,1,1 };
for (int j = 0; j < len; j++)
{
if (map[j].a > map[i].a)
{
count[0]++;
}
if (map[j].c > map[i].c)
{
count[1]++;
}
if (map[j].m > map[i].m)
{
count[2]++;
}
if (map[j].e > map[i].e)
{
count[3]++;
}
}
for (int k = 0; k < 4; k++)
{
int u = k + 1;
for (; u < 4; u++)
{
if (count[k] > count[u]) break;
}
if (u >= 4)
{
map[i].bestsub = ans[k];
map[i].best = count[k];
break;
}
}
}
} int main()
{
int N, M,n;
cin >> N >> M;
n = N;
vector<stu> map;
while (N--)
{
stu s;
cin >> s.id >> s.c >> s.m >> s.e;
s.a = (s.c + s.m + s.e) / 3;
map.push_back(s);
}
getbest(map); string tempid;
while (M--)
{
cin >> tempid;
int i = 0;
for (; i < n; i++)
{
if (map[i].id == tempid)
{
cout << map[i].best << " " << map[i].bestsub << endl;
break;
}
}
if (i >= n)
cout << "N/A" << endl;
}
return 0;
}
05-11 14:49