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

原题链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805260353126400

题目描述:

PAT-BASIC1085——PAT单位排行

知识点:排序

思路:定义两个结构体来保存数据

学生结构体student保存学生编号、学生分数和学校名字。学校结构体保存学校分数和学校学生人数。

注意点:

学校名称需要统一转换为大写或者小写进行比较。

时间复杂度是O(nlogn),其中n为学校总数。空间复杂度是O(N)。

C++代码:

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

using namespace std;

struct student{
	string studentNumber;
	int studentScore;
	string schoolName;
};

struct school{
	double schoolScore;
	int quantity;
};

bool compare(pair<string, school> pair1, pair<string, school> pair2);
string toLower(string s);

int main(){
	int N;
	cin >> N;
	
	student tempStudent;
	string tempStudentNumber;
	int tempStudentScore;
	string tempSchoolName;
	
	vector<student> students;
	for(int i = 0; i < N; i++){
		cin >> tempStudentNumber >> tempStudentScore >> tempSchoolName;
		tempStudent.studentNumber = tempStudentNumber;
		tempStudent.studentScore = tempStudentScore;
		tempSchoolName = toLower(tempSchoolName);
		tempStudent.schoolName = tempSchoolName;
		students.push_back(tempStudent);
	}
	
	map<string, school> schoolMap;
	map<string, school>::iterator it;
	
	for(int i = 0; i < students.size(); i++){
		it = schoolMap.find(students[i].schoolName);
		if(it != schoolMap.end()){
			it->second.quantity++;
			if(students[i].studentNumber[0] == 'B'){
				it->second.schoolScore += 1.0 * students[i].studentScore / 1.5;
			}else if(students[i].studentNumber[0] == 'A'){
				it->second.schoolScore += 1.0 * students[i].studentScore;
			}else if(students[i].studentNumber[0] == 'T'){
				it->second.schoolScore += 1.5 * students[i].studentScore;
			}
		}else{
			school tempSchool;
			tempSchool.quantity = 1;
			if(students[i].studentNumber[0] == 'B'){
				tempSchool.schoolScore = 1.0 * students[i].studentScore / 1.5;
			}else if(students[i].studentNumber[0] == 'A'){
				tempSchool.schoolScore = 1.0 * students[i].studentScore;
			}else if(students[i].studentNumber[0] == 'T'){
				tempSchool.schoolScore = 1.5 * students[i].studentScore;
			}
			schoolMap[students[i].schoolName] = tempSchool;
		}
	}
	
	vector<pair<string, school> > results;
	for(it = schoolMap.begin(); it != schoolMap.end(); it++){
		results.push_back(make_pair(it->first, it->second));
	}
	
	sort(results.begin(), results.end(), compare);
	
	cout << results.size() << endl;
	int index = 1;
	for(int i = 0; i < results.size(); i++){
		if(i > 0 && (int)results[i].second.schoolScore != (int)results[i - 1].second.schoolScore){
			index = i + 1;
		}		
		cout << index << " " << results[i].first << " " << (int)results[i].second.schoolScore << " " << results[i].second.quantity << endl;
	}
	
}

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

string toLower(string s){
	string result = "";
	for(int i = 0; i < s.length(); i++){
		if(s[i] >= 'A' && s[i] <= 'Z'){
			result += s[i] - 'A' + 'a';
		}else{
			result += s[i];
		}
	}
	return result;
}

C++解题报告:

PAT-BASIC1085——PAT单位排行

 

相关文章:

  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
  • 2021-12-16
  • 2021-10-11
猜你喜欢
  • 2021-12-05
  • 2021-10-20
  • 2022-12-23
  • 2022-02-06
  • 2022-02-06
  • 2021-04-22
  • 2022-12-23
相关资源
相似解决方案