三刷
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
string sname;
double avscore;
int num;
bool operator<(const node& t) const{
if(avscore==t.avscore){
if(num==t.num) return sname<t.sname;
else return num<t.num;
}else return avscore>t.avscore;
}
}temp;
int main(){
int n;
cin>>n;
map<string,struct node> mp;
vector<struct node> v;
for(int i=0;i<n;i++){
string tid,schname;
int score;
cin>>tid>>score>>schname;
for(int j=0;j<schname.size();j++) if(isupper(schname[j])) schname[j]=tolower(schname[j]);
mp[schname].sname=schname;
mp[schname].num++;
if(tid[0]=='B') mp[schname].avscore+=score/1.5;
else if(tid[0]=='A') mp[schname].avscore+=score;
else if(tid[0]=='T') mp[schname].avscore+=score*1.5;
}
for(auto it=mp.begin();it!=mp.end();it++) v.push_back(it->second);
for(int i=0;i<v.size();i++){
v[i].avscore=(int)(v[i].avscore);
}
sort(v.begin(),v.end());
cout<<v.size()<<endl;
int k=1;//学习
for(int i=0;i<v.size();i++){
cout<<k<<" "<<v[i].sname<<" "<<v[i].avscore<<" "<<v[i].num<<endl;
if(i+1<v.size()&&v[i].avscore!=v[i+1].avscore) k=i+1+1;
}
return 0;
}
第二次代码
测试点5:
题目要求是:加权分取整数部分,其实是要最后一起取整数,先将他们算过之后保存到double里面,之后遍历一遍数组,给每个学校的分数取整
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
string name;
int num;
double avscore;
};
string chg(string sch){
for(int j=0;j<sch.length();j++){
if(sch[j]>='A'&&sch[j]<='Z') sch[j]=sch[j]+32;
}
return sch;
}
bool cmp(struct node a,struct node b){
if(a.avscore==b.avscore){
if(a.num==b.num) return a.name<b.name;
else return a.num<b.num;
}
else return a.avscore>b.avscore;
}
int main(){
int n;
cin>>n;
map<string,struct node> m;
for(int i=0;i<n;i++){
string id,school;
double score;
cin>>id>>score>>school;
m[chg(school)].num++;
m[chg(school)].name=chg(school);
if(id[0]=='B') m[chg(school)].avscore+=score/1.5;
else if(id[0]=='A') m[chg(school)].avscore+=score;
else if(id[0]=='T') m[chg(school)].avscore+=score*1.5;
}
vector<struct node> v;
for(auto it=m.begin();it!=m.end();it++) v.push_back(it->second);
for(int i=0;i<v.size();i++) v[i].avscore=(int)(v[i].avscore+1e-8);//这里给每个学校统一取整,(+1e8是为了提高精度,因为C++不能精确的保存小数,所以这里手动加上一个很小的数,在官网提交不加也能过,在AcWing上就过不了)
sort(v.begin(),v.end(),cmp);
cout<<v.size()<<endl;
int k=1;//学习
for(int i=0;i<v.size();i++){
cout<<k<<" "<<v[i].name<<" "<<v[i].avscore<<" "<<v[i].num<<endl;
if(i+1<v.size()&&v[i].avscore!=v[i+1].avscore) k=i+1+1;
}
return 0;
}
第一次代码
最后一个测试点总是不过
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
string name;
int avscore,num;
};
string chg(string sch){
for(int j=0;j<sch.length();j++){
if(sch[j]>='A'&&sch[j]<='Z') sch[j]=sch[j]+32;
}
return sch;
}
bool cmp(struct node a,struct node b){
if(a.avscore==b.avscore){
if(a.num==b.num) return a.name<b.name;
else return a.num<b.num;
}
else return a.avscore>b.avscore;
}
int main(){
int n;
cin>>n;
map<string,struct node> m;
for(int i=0;i<n;i++){
string id,school;
int score;
cin>>id>>score>>school;
m[chg(school)].num++;
m[chg(school)].name=chg(school);
if(id[0]=='B') m[chg(school)].avscore+=score/1.5;
else if(id[0]=='A') m[chg(school)].avscore+=score;
else if(id[0]=='T') m[chg(school)].avscore+=score*1.5;
}
vector<struct node> v;
for(auto it=m.begin();it!=m.end();it++) v.push_back(it->second);//写笔记这里是it->second;
sort(v.begin(),v.end(),cmp);
cout<<v.size()<<endl;
int k=1;//学习
for(int i=0;i<v.size();i++){
cout<<k<<" "<<v[i].name<<" "<<v[i].avscore<<" "<<v[i].num<<endl;
if(i+1<v.size()&&v[i].avscore!=v[i+1].avscore) k=i+1+1;
}
return 0;
}
柳神的代码
#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>
#include <unordered_map>
using namespace std;
struct node {
string school;
int tws, ns;
};
bool cmp(node a, node b) {
if (a.tws != b.tws)
return a.tws > b.tws;
else if (a.ns != b.ns)
return a.ns < b.ns;
else
return a.school < b.school;
}
int main() {
int n;
scanf("%d", &n);
unordered_map<string, int> cnt;
unordered_map<string, double> sum;
for (int i = 0; i < n; i++) {
string id, school;
cin >> id;
double score;
scanf("%lf", &score);
cin >> school;
for (int j = 0; j < school.length(); j++)
school[j] = tolower(school[j]);
if (id[0] == 'B')
score = score / 1.5;
else if (id[0] == 'T')
score = score * 1.5;
sum[school] += score;
cnt[school]++;
}
vector<node> ans;
for (auto it = cnt.begin(); it != cnt.end(); it++)
ans.push_back(node{it->first, (int)sum[it->first], cnt[it->first]});
sort(ans.begin(), ans.end(), cmp);
int rank = 0, pres = -1;
printf("%d\n", (int)ans.size());
for (int i = 0; i < ans.size(); i++) {
if (pres != ans[i].tws) rank = i + 1;
pres = ans[i].tws;
printf("%d ", rank);
cout << ans[i].school;
printf(" %d %d\n", ans[i].tws, ans[i].ns);
}
return 0;
}