# PAT 1012. The Best Rank (25)

1.并列排名问题没有考虑，然后再改改，发现依然没有过。
2.平均数是+0.5之后的整数，然后依旧没过。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>

using namespace std;

struct p
{
string StudentID;
int grade_C,grade_M,grade_E;
int grade_A;
}student[20005];

bool cmp_gradeC(p x,p y)
{
return x.grade_C > y.grade_C;
}

bool cmp_gradeM(p x,p y)
{
return x.grade_M > y.grade_M;
}

bool cmp_gradeE(p x,p y)
{
return x.grade_E > y.grade_E;
}

bool cmp_gradeA(p x,p y)
{
return x.grade_A > y.grade_A;
}

int main()
{
int n,m;
while(cin>>n>>m)
{
map<string,int> rank;
map<string,char> courses;
for(int i = 0; i < n; i++)
{
cin>>student[i].StudentID>>student[i].grade_C>>student[i].grade_M>>student[i].grade_E;
student[i].grade_A = (int)((student[i].grade_C + student[i].grade_E + student[i].grade_M)/3.0 + 0.5);
}
sort(student,student+n,cmp_gradeA);
int grade = -1,ranks = 0;
for(int i = 0; i < n; i++)
{
if(student[i].grade_A != grade)
{
ranks = i+1;
}
grade = student[i].grade_A;
rank[student[i].StudentID] = ranks;
courses[student[i].StudentID] = 'A';
}
sort(student,student+n,cmp_gradeC);
grade = -1;
ranks = 0;
for(int i = 0; i < n; i++)
{
if(student[i].grade_C != grade)//一直不过的原因是让i和i-1比较，然后使用了i-1的排名（rank的，而不是本次排序的！）
ranks = i+1;
grade = student[i].grade_C;
if(ranks < rank[student[i].StudentID])
{
rank[student[i].StudentID] = ranks;
courses[student[i].StudentID] = 'C';
}
}
sort(student,student+n,cmp_gradeM);
grade = -1;
ranks = 0;
for(int i = 0; i < n; i++)
{
if(student[i].grade_M != grade)
ranks = i+1;
grade = student[i].grade_M;
if(ranks < rank[student[i].StudentID])
{
rank[student[i].StudentID] = ranks;
courses[student[i].StudentID] = 'M';
}
}
sort(student,student+n,cmp_gradeE);
grade = -1;
ranks = 0;
for(int i = 0; i < n; i++)
{
if(student[i].grade_E != grade)
ranks = i+1;
grade = student[i].grade_E;
if(ranks < rank[student[i].StudentID])
{
rank[student[i].StudentID] = ranks;
courses[student[i].StudentID] = 'E';
}
}
for(int i = 0; i < m; i++)
{
string id;
cin>>id;
map<string ,int>::iterator l_it;
l_it = rank.find(id);
if(l_it == rank.end())
{
cout<<"N/A"<<endl;
}else
{
cout<<rank[id]<<' '<<courses[id]<<endl;
}
}
}
return 0;
}
• 本文已收录于以下专栏：

举报原因： 您举报文章：PAT 1012. The Best Rank (25) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)