PAT-BASIC1080——MOOC期终成绩/PAT-ADVANCED1137——Final Grading

本文分享了PAT-BASIC1080和PAT-ADVANCED1137两道考试题目的解题思路及代码实现。通过定义结构体存储每位考生的多次成绩,利用排序算法对合格者进行排序。详细介绍了问题描述、解题步骤以及时间空间复杂度分析。
摘要由CSDN通过智能技术生成

我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED

原题链接:

PAT-BASIC1080:https://pintia.cn/problem-sets/994805260223102976/problems/994805261493977088

PAT-ADVANCED1137:https://pintia.cn/problem-sets/994805342720868352/problems/994805345401028608

题目描述:

PAT-BASIC1080:

PAT-ADVANCED1137:

知识点:排序

思路:定义一个结构体scores来保存每个人的3次成绩和总分

一开始用一个map<string, scores>来保存每个人的成绩。为了排序的方便,将能获得合格证书的同学放入一个新的vector<pair<string, scores> >的集合里,用sort函数自定义比较函数来排序。

时间复杂度是O(nlogn),其中n为能获得合格证书的同学的人数。空间复杂度是O(P)。

C++代码:

#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>

using namespace std;

struct scores {
	int gP;
	int gMidTerm;
	int gFinal;
	int g;
};

bool compare(pair<string, scores> pair1, pair<string, scores> pair2);

int main() {
	int P, M, N;
	cin >> P >> M >> N;

	map<string, scores> messages;
	map<string, scores>::iterator it;

	scores tempScores;
	string tempName;
	int tempScore;
	for(int i = 0; i < P; i++) {
		cin >> tempName >> tempScore;
		if(tempScore >= 200) {
			tempScores.gP = tempScore;
			tempScores.gMidTerm = -1;
			tempScores.gFinal = -1;
			messages[tempName] = tempScores;
		}
	}

	for(int i = 0; i < M; i++) {
		cin >> tempName >> tempScore;
		it = messages.find(tempName);
		if(it != messages.end()) {
			it->second.gMidTerm = tempScore;
		}
	}

	for(int i = 0; i < N; i++) {
		cin >> tempName >> tempScore;
		it = messages.find(tempName);
		if(it != messages.end()) {
			it->second.gFinal = tempScore;
		}
	}

	vector<pair<string, scores> > results;
	for(it = messages.begin(); it != messages.end(); it++) {
		int gMidTerm = it->second.gMidTerm;
		int gFinal = it->second.gFinal;
		double g;
		if(gMidTerm > gFinal){
			g = gMidTerm * 0.4 + gFinal * 0.6;
		}else{
			g = gFinal;
		}
		int trueG = (int) g;
		if(g - trueG >= 0.5){
			trueG++;
		}
		it->second.g = trueG;
		if(trueG >= 60){
			results.push_back(make_pair(it->first, it->second));
		}
	}
	
	sort(results.begin(), results.end(), compare);
	
	for(int i = 0; i < results.size(); i++){
		cout << results[i].first << " " << results[i].second.gP << " " << results[i].second.gMidTerm 
			<< " " << results[i].second.gFinal << " " << results[i].second.g << endl;
	}
	
}

bool compare(pair<string, scores> pair1, pair<string, scores> pair2){
	if(pair1.second.g == pair2.second.g){
		if(pair1.first.compare(pair2.first) >= 0){
			return false;
		}else{
			return true;
		}
	}else{
		if(pair1.second.g <= pair2.second.g){
			return false;
		}else{
			return true;
		}
	}
}

C++解题报告:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值