四门功课,输出排名最高的是哪个
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 999999Sample Output
1 C 1 M 1 E 1 A 3 A N/A
代码比较冗余。
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <map>
#include <algorithm>
using namespace std;
class STUDENT
{
public:
string id;
int c_score;
int m_score;
int e_score;
int a_score;
int c_rank;
int m_rank;
int e_rank;
int a_rank;
int best_rank;
string best_course;
STUDENT()
{
c_score = 0;
m_score = 0;
e_score = 0;
a_score = 0;
c_rank = 0;
m_rank = 0;
e_rank = 0;
a_rank = 0;
best_rank = 0;
}
int GetScore(int course);
void SetRank(int course, int rank);
};
int STUDENT::GetScore(int course)
{
switch(course)
{
case 1:
return c_score;
case 2:
return m_score;
case 3:
return e_score;
case 4:
return a_score;
default:
return 0;
}
}
void STUDENT::SetRank(int course, int rank)
{
switch(course)
{
case 1:
c_rank = rank;
return;
case 2:
m_rank = rank;
return;
case 3:
e_rank = rank;
return;
case 4:
a_rank = rank;
return;
default:
return;
}
}
bool greate_c_score(const STUDENT& s1,const STUDENT& s2)
{
return s1.c_score > s2.c_score;
}
bool greate_m_score(const STUDENT& s1,const STUDENT& s2)
{
return s1.m_score > s2.m_score;
}
bool greate_e_score(const STUDENT& s1,const STUDENT& s2)
{
return s1.e_score > s2.e_score;
}
bool greate_a_score(const STUDENT& s1,const STUDENT& s2)
{
return s1.a_score > s2.a_score;
}
void FunRank(vector<STUDENT> &VStu, int course)
{
int forword = -1;
int irank = 0;
int realrank = 0;
vector<STUDENT>::iterator iter1 = VStu.begin();
while(iter1 != VStu.end())
{
++realrank;
if (forword != (*iter1).GetScore(course))
{
irank = realrank;
(*iter1).SetRank(course,realrank);
forword = (*iter1).GetScore(course);
}
else
{
(*iter1).SetRank(course,irank);
}
iter1++;
}
}
int main()
{
int N,M;
cin>>N>>M;
map<string,STUDENT> StuMap;
vector<STUDENT> VStu;
vector<string> VStuid;
while(N--)
{
STUDENT stu;
cin>>stu.id>>stu.c_score>>stu.m_score>>stu.e_score;
stu.a_score = (stu.c_score + stu.m_score + stu.e_score)/3;
VStu.push_back(stu);
}
sort(VStu.begin(), VStu.end(), greate_c_score);
FunRank(VStu,1);
sort(VStu.begin(), VStu.end(), greate_m_score);
FunRank(VStu,2);
sort(VStu.begin(), VStu.end(), greate_e_score);
FunRank(VStu,3);
sort(VStu.begin(), VStu.end(), greate_a_score);
FunRank(VStu,4);
vector<STUDENT>::iterator iter = VStu.begin();
while(iter != VStu.end())
{
if ((*iter).e_rank < (*iter).m_rank)
{
(*iter).best_rank = (*iter).e_rank;
(*iter).best_course = "E";
}
else
{
(*iter).best_rank = (*iter).m_rank;
(*iter).best_course = "M";
}
if ((*iter).best_rank >= (*iter).c_rank)
{
(*iter).best_rank = (*iter).c_rank;
(*iter).best_course = "C";
}
if ((*iter).best_rank >= (*iter).a_rank)
{
(*iter).best_rank = (*iter).a_rank;
(*iter).best_course = "A";
}
StuMap[(*iter).id] = *iter;
iter++;
}
while(M--)
{
string str;
cin>>str;
VStuid.push_back(str);
}
for (int i = 0; i < VStuid.size(); i++)
{
map<string,STUDENT>::iterator iter = StuMap.find(VStuid[i]);
if (iter != StuMap.end())
{
cout<<(iter->second).best_rank<<" "<<(iter->second).best_course<<endl;
}
else
{
cout<<"N/A"<<endl;
}
}
return 0;
}