PAT乙级1085 PAT单位排行 (25 分)

本程序通过结构体数组存储学生的姓名、最高分、学校等信息,并使用map来跟踪学校出现次数,为每个学校分配唯一编号。根据学生名字首字母确定最低分,汇总各校总分及人数,最终按特定规则对学校进行排序并输出。

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

https://pintia.cn/problem-sets/994805260223102976/problems/994805260353126400
一个学校的结构体数组,下标从1开始分配
读入数据时,用map检测学校是否出现过,如果没有出现过,就分配新的下标,否则就用以前的下标

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
struct node1{
	string name, school;
	double min, max; 
};
struct node2{
	string school_name;
	double score = 0.0;
	int count = 0;
};
string Atoa(string a){
	for(int i = 0; i < a.size();i++)
		a[i] = (a[i] >= 'A' && a[i] <= 'Z') ? (a[i]-'A'+'a') : a[i];
	return a;
}
bool cmp(node2 a, node2 b){
	if((int)a.score != (int)b.score)	return a.score > b.score;
	else if(a.count != b.count)	return a.count < b.count;
	else	return a.school_name < b.school_name;
}
map<string, int> match;
node1 stu[100010];
node2 sch[100010];
int main(){
	int N, score, sub = 0, tmp;
	string t_num, t_school;
	cin >> N;
	for(int i = 0; i < N; i++){
		cin >> stu[i].name >> stu[i].max >> stu[i].school;
		stu[i].school = Atoa(stu[i].school);
		if(stu[i].name[0] == 'B')	stu[i].min = stu[i].max / 1.5;
		else if(stu[i].name[0] == 'A')	stu[i].min = stu[i].max;
		else if(stu[i].name[0] == 'T')	stu[i].min = stu[i].max * 1.5;
		if(!match[stu[i].school]){
			tmp = ++sub;
			match[stu[i].school] = tmp;
		}
		else
			tmp = match[stu[i].school];
		sch[tmp].school_name = stu[i].school;
		sch[tmp].score += stu[i].min;
		sch[tmp].count++;	
	}
	sort(sch+1, sch+sub+1, cmp);
	cout << sub << endl;
	for(int i = 1; i <= sub; i++){
		int cnt = 0;
		for(int j = i; j <= sub && ((int)sch[i].score == (int)sch[j].score) ; j++, cnt++)
			cout << i << " " << sch[j].school_name << " " << (int)sch[j].score << " " << sch[j].count << endl;
		i += --cnt;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值