天天看點

PAT-BASIC1015——德才論/PAT-ADVANCED1062——Talent and Virtue

我的PAT-BASIC代碼倉:https://github.com/617076674/PAT-BASIC

我的PAT-ADVANCED代碼倉:https://github.com/617076674/PAT-ADVANCED

原題連結:

PAT-BASIC1015:https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312

PAT-ADVANCED1062:https://pintia.cn/problem-sets/994805342720868352/problems/994805410555346944

題目描述:

PAT-BASIC1015:

PAT-BASIC1015——德才論/PAT-ADVANCED1062——Talent and Virtue

PAT-ADVANCED1062:

PAT-BASIC1015——德才論/PAT-ADVANCED1062——Talent and Virtue

知識點:結構體

思路:給每一個學生結構體新增一個level變量來标記其屬于哪一分類

自定義結構體的排序規則,并用sort函數進行排序。

時間複雜度是O(nlogn),其中n是輸入的學生記錄數。空間複雜度是O(n)。

C++代碼:

​​#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct Student {
	int number;
	int morality;
	int talent;
	int level;
};

bool compareStudent(Student student1, Student student2) {
	if (student1.level == student2.level) {
		if (student1.morality + student1.talent == student2.morality + student2.talent) {
			if (student1.morality == student2.morality) {
				if (student1.number < student2.number) {
					return true;
				} else {
					return false;
				}
			} else {
				if (student1.morality > student2.morality) {
					return true;
				} else {
					return false;
				}
			}
		} else {
			if (student1.morality + student1.talent > student2.morality + student2.talent) {
				return true;
			} else {
				return false;
			}
		}
	} else {
		if (student1.level < student2.level) {
			return true;
		} else {
			return false;
		}
	}
}

int main() {
	int count;
	int minScore;
	int priorityScore;

	scanf("%d%d%d", &count, &minScore, &priorityScore);

	vector<Student> students;
	Student student;

	for (int i = 0; i < count; i++) {
		int number;
		int morality;
		int talent;

		scanf("%d%d%d", &number, &morality, &talent);

		if (morality >= minScore && talent >= minScore) {
			student.number = number;
			student.morality = morality;
			student.talent = talent;
			if (morality >= priorityScore && talent >= priorityScore) {
				student.level = 1;
			} else if (morality >= priorityScore && talent < priorityScore) {
				student.level = 2;
			} else if (morality < priorityScore && talent < priorityScore && morality >= talent) {
				student.level = 3;
			} else {
				student.level = 4;
			}
			students.push_back(student);
		}
	}
	sort(students.begin(), students.end(), compareStudent);

	printf("%d\n", students.size());
	for (int i = 0; i < students.size(); i++) {
		printf("%d %d %d", students[i].number, students[i].morality, students[i].talent);
		if (i != students.size() - 1) {
			printf("\n");
		}
	}
	return 0;
}

           

C++解題報告:

PAT-BASIC1015——德才論/PAT-ADVANCED1062——Talent and Virtue