按照题意写完提交最后一个测试点过不去
Score = ScoreB/1.5 + ScoreA + ScoreT*1.5
因为score是整型而1.5是小数,乘除的过程中会出现精度的问题
所以我的解决方案是先将B,A,T三个级别的分数分开存放,输入数据处理好后再按照系数算出score的值
代码如下
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<map>
using namespace std;
struct school{
string school;
int Bscore;
int Ascore;
int Tscore;
int score;
int Rank;
int cnt;
}p[100005];
string change(string str)
{
for(int i=0;i<str.size();i++)
{
if(str[i]>='A' && str[i]<='Z')
{
str[i] += ('a'-'A');
}
}
return str;
}
bool cmp(school a, school b)
{
if(a.score != b.score){
return a.score > b.score;
}
else if(a.cnt != b.cnt)
{
return a.cnt < b.cnt;
}
else if(a.school != b.school)
{
return a.school < b.school;
}
}
int main()
{
int n;
scanf("%d",&n);
if(n==0) return 0;
map<string, int> mp;
string num;
int score;
string school;
int cnt = 0;
for(int i=0;i<n;i++)
{
cin>>num>>score>>school;
school = change(school);
//cout<<school<<endl;
if(mp.count(school) == 0)
{
mp[school] = cnt++;
p[ mp[school] ].school = school;
}
p[ mp[school] ].cnt++;
if(num[0]=='B') {
p[ mp[school] ].Bscore += score;
}
else if(num[0]=='A') {
p[ mp[school] ].Ascore += score;
}
else if(num[0]=='T') {
p[ mp[school] ].Tscore += score;
}
}
for(int i=0;i<cnt;i++)
{
p[i].score = (p[i].Bscore*2)/3 + p[i].Ascore + (p[i].Tscore*3)/2;
}
sort(p,p+cnt,cmp);
for(int i=0;i<cnt;i++)
{
if(i==0)
{
p[i].Rank = 1;
}
else if(p[i].score == p[i-1].score)
{
p[i].Rank = p[i-1].Rank;
}
else
{
p[i].Rank = i+1;
}
}
printf("%d\n",cnt);
for(int i=0;i<cnt;i++)
{
cout<<p[i].Rank<<" "<<p[i].school<<" "<<p[i].score<<" "<<p[i].cnt<<endl;
}
}