PAT (Advanced Level) 1141 PAT Ranking of Institutions

本文介绍了一种针对学生在不同类别下得分的计算方法,并通过整合这些得分来计算最终的权重分数(TWS)。文章提供了完整的C++代码实现,包括如何处理学生信息输入、按学校分组、计算总分及权重分数,以及最后按照特定规则对学生进行排序的过程。

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

这题有个精度的问题,如果每输入一个学生,进行相应的tws计算,由于只取int部分,数据大的时候,误差可能比较大,所以第一遍最后一个case过不了,最后改成求和再计算。 

#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
#include<cstdlib>
using namespace std;

int n;
map<string,int> sc2id;

struct School{
	string name;
	int tws = 0, ns = 0, a = 0, b = 0, t = 0;
	School(){
	}
	bool operator < (const School &a) const{
		if(tws == a.tws){
			if(ns == a.ns){
				return name < a.name;
			}
			return ns < a.ns;
		}
		else return tws > a.tws;
	}
};

vector<School> school;

void lower(string &c){
	int i = 0;
	while(c[i] != '\0'){
		if(c[i] < 'a')
			c[i] = c[i] - 'A' + 'a';
		++i;
	}
}
int main(){
	scanf("%d",&n);
	char id[7];
	string sc;
	int w, cnt = 0;
	for(int i = 0; i < n; ++i){
		scanf("%s %d",id,&w);
		cin >> sc;
		lower(sc);
		int sid;
		if(sc2id.find(sc) != sc2id.end()){
			sid = sc2id[sc];
		}
		else{
			sc2id[sc] = cnt;
			sid = cnt++;
			School tmp;
			tmp.name = sc;
			school.push_back(tmp);
		}
		if(id[0] == 'B') school[sid].b += w;
		else if(id[0] == 'A') school[sid].a += w;
		else school[sid].t += w;
		++school[sid].ns;
	}
	for(int i = 0; i < school.size(); ++i)
		school[i].tws = (int)(school[i].a + school[i].b/1.5 + school[i].t*1.5);
	sort(school.begin(),school.end());
	printf("%d\n",school.size());
	cnt = 1;
	for(int i = 0; i < school.size(); ++i){
		if(i != 0){
			if(school[i].tws != school[i-1].tws) cnt = i+1;
		}
		printf("%d ",cnt);
		cout << school[i].name;
		printf(" %d %d\n",school[i].tws,school[i].ns);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值