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-ADVANCED1062:
知识点:结构体
思路:给每一个学生结构体新增一个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++解题报告: