PAT-BASIC1095——解码PAT准考证/PAT-ADVANCED1153——Decode Registration Card of PAT

我的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-BASIC1095——解码PAT准考证/PAT-ADVANCED1153——Decode Registration Card of PAT_第1张图片

PAT-ADVANCED1153:

PAT-BASIC1095——解码PAT准考证/PAT-ADVANCED1153——Decode Registration Card of PAT_第2张图片

知识点:排序

思路:根据题目描述,分三种情况进行讨论

(1)类型1查询

为避免遇到类型1查询时都对参加某种类型考试的的学生进行一次排序操作,首先定义一个大小为3的vector类型数组filterByLevel[],其中索引为0的位置存储参加"A"级考试的学生,索引为1的位置存储参加"B"级考试的学生,索引为2的位置存储参加"T"级考试的学生。

然后将这3个位置的vector集合都排好序,遇到类型1查询,不再需要排序,只需输出即可。

(2)类型2查询

事先用一个vector类型的数组filterBySiteNumber[]按考场编号存储学生。

遇到查询2时,只需输出相应位置的考生人数和考生总分即可。

(3)类型3查询

考虑到日期由6位数组成,如果用数组存储,需要一个大小为1000000的vector型数组,超出了空间限制。而我们又不需要日期有序,因此选择unordered_map >型变量filterByDate来存储每个日期对应的学生信息。

遇到查询3时,开一个vector型的数组tempFilterBySiteNumber[]按考场编号存储当前日期下的学生信息。同时还需要一个新的结构体place,其中存储考场编号和考场考生数量。我们只需遍历tempFilterBySiteNumber,将每个考场的信息存进一个vector型变量places里,再对places按题述要求进行排序,再输出即可。

时间复杂度与查询信息有关。空间复杂度至少是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++解题报告:

PAT-BASIC1095——解码PAT准考证/PAT-ADVANCED1153——Decode Registration Card of PAT_第3张图片

 

你可能感兴趣的:(PAT甲级真题题解,PAT乙级真题题解)