我的PAT-BASIC代码仓:https://github.com/617076674/PAT-BASIC
我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED
原题链接:
PAT-BASIC1095:https://pintia.cn/problem-sets/994805260223102976/problems/1071786104348536832
PAT-ADVANCED1153:https://pintia.cn/problem-sets/994805342720868352/problems/1071785190929788928
题目描述:
PAT-BASIC1095:
PAT-ADVANCED1153:
知识点:排序
(1)类型1查询
为避免遇到类型1查询时都对参加某种类型考试的的学生进行一次排序操作,首先定义一个大小为3的vector
然后将这3个位置的vector
(2)类型2查询
事先用一个vector
遇到查询2时,只需输出相应位置的考生人数和考生总分即可。
(3)类型3查询
考虑到日期由6位数组成,如果用数组存储,需要一个大小为1000000的vector
遇到查询3时,开一个vector
时间复杂度与查询信息有关。空间复杂度至少是O(N)。
C++代码:
#include
#include
#include
#include
#include
using namespace std;
struct student {
char number[14];
int score, site, date;
};
struct place {
int siteNumber, people;
place(int _siteNumber, int _people) {
siteNumber = _siteNumber;
people = _people;
}
};
int N, M;
vector filterBySiteNumber[1000], filterByLevel[3]; //将考生按考场编号进行分类;将考生按考试等级进行分类,0代表A级,1代表B级,2代表T级
unordered_map > filterByDate; //将考生按日期进行分类
bool cmp1(student s1, student s2);
bool cmp2(place p1, place p2);
int main() {
scanf("%d %d", &N, &M);
student students[N];
for(int i = 0; i < N; i++) {
scanf("%s %d", students[i].number, &students[i].score);
students[i].site = 0;
for(int j = 1; j <= 3; j++) {
students[i].site = students[i].site * 10 + students[i].number[j] - '0';
}
if(students[i].number[0] == 'A') {
filterByLevel[0].push_back(students[i]);
} else if(students[i].number[0] == 'B') {
filterByLevel[1].push_back(students[i]);
} else {
filterByLevel[2].push_back(students[i]);
}
students[i].date = 0;
for(int j = 4; j <= 9; j++) {
students[i].date = students[i].date * 10 + students[i].number[j] - '0';
}
filterBySiteNumber[students[i].site].push_back(students[i]);
filterByDate[students[i].date].push_back(students[i]);
}
for(int i = 0; i < 3; i++) {
sort(filterByLevel[i].begin(), filterByLevel[i].end(), cmp1);
}
for(int i = 1; i <= M; i++) {
int kind;
scanf("%d", &kind);
printf("Case %d: %d ", i, kind);
if(kind == 1) {
char type[2];
scanf("%s", type);
printf("%s\n", type);
int choose;
if(type[0] == 'A') {
choose = 0;
} else if(type[0] == 'B') {
choose = 1;
} else {
choose = 2;
}
for(int j = 0; j < filterByLevel[choose].size(); j++) {
printf("%s %d\n", filterByLevel[choose][j].number, filterByLevel[choose][j].score);
}
if(filterByLevel[choose].size() == 0) {
printf("NA\n");
}
} else if(kind == 2) {
int query;
scanf("%d", &query);
printf("%d\n", query);
int sum = 0;
for(int j = 0; j < filterBySiteNumber[query].size(); j++) {
sum += filterBySiteNumber[query][j].score;
}
if(filterBySiteNumber[query].size() != 0) {
printf("%d %d\n", filterBySiteNumber[query].size(), sum);
} else {
printf("NA\n");
}
} else {
int queryDate;
scanf("%d", &queryDate);
printf("%06d\n", queryDate);
vector tempFilterBySiteNumber[1000];
for(int j = 0; j < filterByDate[queryDate].size(); j++) {
tempFilterBySiteNumber[filterByDate[queryDate][j].site].push_back(filterByDate[queryDate][j]);
}
vector places;
for(int j = 101; j < 1000; j++) {
if(tempFilterBySiteNumber[j].size() > 0) {
places.push_back(place(j, tempFilterBySiteNumber[j].size()));
}
}
sort(places.begin(), places.end(), cmp2);
for(int j = 0; j < places.size(); j++) {
printf("%d %d\n", places[j].siteNumber, places[j].people);
}
if(places.size() == 0) {
printf("NA\n");
}
}
}
return 0;
}
bool cmp1(student s1, student s2) {
if(s1.score == s2.score) {
return strcmp(s1.number, s2.number) < 0;
} else {
return s1.score > s2.score;
}
}
bool cmp2(place p1, place p2) {
if(p1.people == p2.people) {
return p1.siteNumber < p2.siteNumber;
}
return p1.people > p2.people;
}
C++解题报告: