这题有个精度的问题,如果每输入一个学生,进行相应的tws计算,由于只取int部分,数据大的时候,误差可能比较大,所以第一遍最后一个case过不了,最后改成求和再计算。
#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
#include<cstdlib>
using namespace std;
int n;
map<string,int> sc2id;
struct School{
string name;
int tws = 0, ns = 0, a = 0, b = 0, t = 0;
School(){
}
bool operator < (const School &a) const{
if(tws == a.tws){
if(ns == a.ns){
return name < a.name;
}
return ns < a.ns;
}
else return tws > a.tws;
}
};
vector<School> school;
void lower(string &c){
int i = 0;
while(c[i] != '\0'){
if(c[i] < 'a')
c[i] = c[i] - 'A' + 'a';
++i;
}
}
int main(){
scanf("%d",&n);
char id[7];
string sc;
int w, cnt = 0;
for(int i = 0; i < n; ++i){
scanf("%s %d",id,&w);
cin >> sc;
lower(sc);
int sid;
if(sc2id.find(sc) != sc2id.end()){
sid = sc2id[sc];
}
else{
sc2id[sc] = cnt;
sid = cnt++;
School tmp;
tmp.name = sc;
school.push_back(tmp);
}
if(id[0] == 'B') school[sid].b += w;
else if(id[0] == 'A') school[sid].a += w;
else school[sid].t += w;
++school[sid].ns;
}
for(int i = 0; i < school.size(); ++i)
school[i].tws = (int)(school[i].a + school[i].b/1.5 + school[i].t*1.5);
sort(school.begin(),school.end());
printf("%d\n",school.size());
cnt = 1;
for(int i = 0; i < school.size(); ++i){
if(i != 0){
if(school[i].tws != school[i-1].tws) cnt = i+1;
}
printf("%d ",cnt);
cout << school[i].name;
printf(" %d %d\n",school[i].tws,school[i].ns);
}
return 0;
}