【1141】PAT Ranking of Institutions (25分)【排序 map】
【摘要】
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<math.h>#include<string.h>#include<algorithm> #include<map>#inclu...
-
#include<iostream>
-
#include<stdio.h>
-
#include<stdlib.h>
-
#include<math.h>
-
#include<string.h>
-
#include<algorithm>
-
#include<map>
-
#include<vector>
-
#include<queue>
-
#include<unordered_map>
-
#include<string>
-
#include<cctype>
-
using namespace std;
-
//用两个map标记学校和(加权总分,参赛人数),保存在结构体中进行排序后输出
-
struct node{
-
string school;
-
int tws,ns;//加权总分 参赛人数
-
};
-
//cmp对结构体进行排序
-
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);//n个学生
-
unordered_map<string,int>cnt;
-
//cnt存储某学校名称对应的参赛人数
-
unordered_map<string,double>sum;
-
//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++){
-
struct node a={it->first,(int)sum[it->first],cnt[it->first]};
-
//学校字符串 加权总分 学校参赛人数
-
//注意加权总分要取整,否则会有3个错误点
-
ans.push_back(a);
-
}
-
sort(ans.begin(),ans.end(),cmp);
-
int rank=0,pres=-1;
-
//pres表示前一个学校的加权总分
-
//如果pres和当前学校的加权总分不同,说明rank等于数组下标+1,否则rank不变
-
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);//加权总分,参赛人数
-
}
-
system("pause");
-
return 0;
-
}
文章来源: andyguo.blog.csdn.net,作者:山顶夕景,版权归原作者所有,如需转载,请联系作者。
原文链接:andyguo.blog.csdn.net/article/details/104049285
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)