1085 PAT单位排行 (25 分)
#include <algorithm>
#include <string.h>
#include <cctype>
#include <map>
#include <cstdio>
using namespace std;
struct School
{
char name[7]; // 学校名
int grade; // 学校加权总分
int people; // 学校人数
int rank; // 学校排名
}S[100001];
int cmp(School s1, School s2)
{ /* 按题目要求进行排序 */
if (s1.grade != s2.grade)
return s1.grade > s2.grade;
else
{
if (s1.people != s2.people)
return s1.people < s2.people;
else
return strcmp(s1.name, s2.name) < 0;
}
}
void ToLower(char *s)
{ /* 大小写转换 */
for (int i = 0; i < strlen(s); i++)
s[i] = tolower(s[i]);
}
int main(int argc, char const *argv[])
{ /* 能用scanf的地方就用scanf,能用printf的地方就用printf,不然会超时 */
int N, idx = 0;
double grade;
char ID[7], name[7];
scanf("%d", &N);
map <string, double> mGrade, mNum, mName; // 保存学校成绩,和学校人数,和学校姓名
for (int i = 0; i < N; i++)
{
scanf("%s %lf %s", ID, &grade, name);
ToLower(name); // 全部转为小写
if (!mName[name])
{ /* 记录不重复的名字和个数 */
strcpy(S[idx++].name, name);
mName[name]++;
}
mNum[name]++;
if (ID[0] == 'T')
mGrade[name] += grade*1.5;
else if (ID[0] == 'A')
mGrade[name] += grade;
else
mGrade[name] += grade/1.5;
}
for (int i = 0; i < idx; i++)
{
S[i].grade = (int)mGrade[S[i].name];
S[i].people = mNum[S[i].name];
}
sort(S, S+idx, cmp);
printf("%d\n", idx); // 输出单位个数
S[0].rank = 1;
cout << 1 << " " << S[0].name << " " << S[0].grade << " " << S[0].people << endl;
for (int i = 1; i < idx; i++)
{
if (S[i].grade == S[i-1].grade) // 如果成绩相同
S[i].rank = S[i-1].rank;
else
S[i].rank = i+1;
printf("%d %s %d %d\n",S[i].rank, S[i].name, S[i].grade, S[i].people);
}
return 0;
}