【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>
#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;
}
发布了179 篇原创文章 · 获赞 12 · 访问量 6970

猜你喜欢

转载自blog.csdn.net/qq_35812205/article/details/104049285