1141 PAT Ranking of Institutions

本文展示了一个使用C++进行编程竞赛的示例代码,通过处理学生数据来演示如何运用结构体、排序算法和哈希表等数据结构与算法解决实际问题。代码中包括了学生姓名、总加权分数和学生数量的处理,使用了标准输入输出和字符串操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<bits/stdc++.h>
using namespace std;
const int MAXN=100010;
struct schnode{
	string name;
	int tws;
	int ns;
}node[MAXN];
bool cmp(schnode a,schnode 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.name<b.name;
}
int main(){
	//freopen("in.txt","r",stdin);
	int n;cin>>n;
	map<string,double> hashtws;
	map<string,int> hashns;
	for(int i=0;i<n;i++){
		string stuname;
		int score;
		string schname;
		cin>>stuname>>score>>schname;
		transform(schname.begin(),schname.end(),schname.begin(),::tolower);
		hashns[schname]++;
		if(stuname[0]=='T'){
			hashtws[schname]+=score*1.5;
		}else if(stuname[0]=='A'){
			hashtws[schname]+=score;
		}else{
			hashtws[schname]+=score/1.5;
		}
	}
	int t=0;
	for(auto it=hashtws.begin();it!=hashtws.end();it++){
		node[t].name=it->first;
		node[t++].tws=it->second;
	}
	int tt=0;
	for(auto it=hashns.begin();it!=hashns.end();it++){
		node[tt].name=it->first;
		node[tt++].ns=it->second;
	}
	cout<<max(tt,t)<<endl;
	sort(node,node+tt,cmp);int rank=1;cout<<rank<<' ';
	cout<<node[0].name<<' '<<(int)node[0].tws<<' '<<node[0].ns<<endl;
	for(int i=1;i<tt;i++){
		if(node[i].tws==node[i-1].tws){
			cout<<rank<<' ';
		}else{
			rank=i+1;
			cout<<rank<<' ';
		}
		cout<<node[i].name<<' '<<(int)node[i].tws<<' '<<node[i].ns<<endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值