2020PAT甲级春季考试题目及解答

总结:

按这个出题的风格,历年的题应该没啥常考价值了,没有模板题,都得靠平时编码积累。


|
|
|
|

我的专栏中还有其他年份未公布的PAT真题,欢迎大家查阅

历年未开放的PAT真题
|
|
|
|
|

7-1 Prime Day (20分)

在这里插入图片描述

Sample Input 1:

20190523

Sample Output 1:

20190523 Yes
0190523 Yes
190523 Yes
90523 Yes
0523 Yes
523 Yes
23 Yes
3 Yes
All Prime!
Sample Input 2:
20191231
Sample Output 2:
20191231 Yes
0191231 Yes
191231 Yes
91231 No
1231 Yes
231 No
31 Yes
1 No

我的代码:

#include<bits/stdc++.h>
using namespace std;
bool isprime(int x) {
	if (x < 2) return false;
	for (int i = 2; i*i <= x; i++) {
		if (x%i == 0) return false;
	}
	return true;
}
int main() {
#ifdef ONLINE_JUDGE
#else
	freopen("Text.txt", "r", stdin);
#endif // DEBUG
	string str;
	cin >> str;
	long long num;
	int len = str.length();
	int flag = 1;
	for (int i = 0; i < len; i++) {
		string s = str.substr(i, len - i);
		printf("%s ", s.c_str());
		num = stoi(s);
		if (isprime(num)) {
			printf("Yes\n");
		}
		else {
			printf("No\n");
			flag = 0;
		}
	}
	if (flag == 1) {
		printf("All Prime!");
	}
}

7-2 The Judger

在这里插入图片描述

Sample Input 1:

101 42
4 5
59 34 67 9 7
17 9 8 50 7
25 92 43 26 37
76 51 1 41 40

Sample Output 1:

Round #4: 1 is out.
Round #5: 3 is out.
Winner(s): 2 4

Sample Input 2:

42 101
4 5
59 34 67 9 7
17 9 18 50 49
25 92 58 1 39
102 32 2 6 41

Sample Output 2:

Round #1: 4 is out.
Round #3: 2 is out.
Round #4: 1 is out.
Round #5: 3 is out.
No winner.

思路:两个unordered_set s,ans;s来存已经输入的元素,ans来存所有可以得到的差。

#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
unordered_set<int> s,ans;
int A[12][1009];
int N, M;
bool visited[15];
int main() {
#ifdef ONLINE_JUDGE
#else
	freopen("Text.txt", "r", stdin);
#endif // DEBUG
	int num1, num2,sum=0;
	scanf("%d %d", &num1, &num2);
	int diff = abs(num1-num2);//记录其差值
	ans.insert(diff);
	s.insert(num1); s.insert(num2);
	cin >> N >> M;
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= M; j++) {
			scanf("%d", &A[i][j]);
		}
	}
	for (int i = 1; i <= M; i++) {
		for (int j = 1; j <= N; j++) {
			if (visited[j] == true) continue;
			//表示以及存在这个数字了,或者给出了错误的数字(不能由先前给出的两个数字求差得出)
			if (s.find(A[j][i])!=s.end() || ans.find(A[j][i])==ans.end()) {
				visited[j] = true;//踢出去
				printf("Round #%d: %d is out.\n",i,j);
				sum++;//记录胜利者,若为0就表示没有胜利者
				continue;
			}
			s.insert(A[j][i]);
			//每进来一个元素跟新s和ans;
			for (auto it = s.begin(); it != s.end(); it++) {
				ans.insert(abs((*it) - A[j][i]));
			}
		}
	}
	if (sum == N) {
		printf("No winner.");
		return 0;
	}
	else {
		printf("Winner(s):");
		for (int i = 1; i <= N; i++) {
			if (visited[i] == false) printf(" %d", i);
		}
	}
}

7-3 Safari Park

在这里插入图片描述

Sample Input:

6 8 3
2 1
1 3
4 6
2 5
2 4
5 4
5 6
3 6
5
1 2 3 3 1 2
1 2 3 4 5 6
4 5 6 6 4 5
2 3 4 2 3 4
2 2 2 2 2 2

Sample Output:

Yes
Error: Too many species.
Yes
No
Error: Too few species.

思路:领接表存储这个图,然后判断相邻节点之间有没有相同的动物

Input Sample

6 8 3
2 1
1 3
4 6
2 5
2 4
5 4
5 6
3 6
5
1 2 3 3 1 2
1 2 3 4 5 6
4 5 6 6 4 5
2 3 4 2 3 4
2 2 2 2 2 2

Output Sample

Yes
Error: Too many species.
Yes
No
Error: Too few species.
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
vector<int> G[509];
unordered_map<int, bool> mp;
int ani[509];
int main() {
#ifdef ONLINE_JUDGE
#else
	freopen("Text.txt", "r", stdin);
#endif // DEBUG
	int N, M, K;
	cin >> N >> M >> K;
	for (int i = 0; i < M; i++) {
		int u, v;
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	int num; cin >> num;
	for (int j = 0; j < num; j++) {
		int sum = 0;
		for (int i = 1; i <= N; i++) {
			cin >> ani[i];
			if (mp[ani[i]] == false) {
				sum++;
				mp[ani[i]] = true;
			}
		}
		mp.clear();
		if (sum > K) {
			cout << "Error: Too many species." << endl;
			continue;
		}
		if (sum < K) {
			cout << "Error: Too few species." << endl;
			continue;
		}
		int flag = 1;
		for (int i = 1; i <= N; i++) {
			int len = G[i].size();
			for (int k = 0; k < len; k++) {
				if (ani[G[i][k]] == ani[i]) {
					flag = 0;
					break;
				}
			}
		}
		if (flag == 1) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
}

7-4 Replacement Selection

在这里插入图片描述

Sample Input:

13 3
81 94 11 96 12 99 17 35 28 58 41 75 15

Sample Output:

11 81 94 96 99
12 17 28 35 41 58 75
15

**模拟 Replace Selection Sort 的过程,就是说内存不够大只能一次排几个数balabala
好长的英文题,好长好长,不过看看例子就懂了,输入N(数组的长度,输入的元素个数)。K表示一次最多可以排序的个数。
我模拟模拟这个过程:
一开始内存中肯定只有 81 94 11 三个数 排序后就是 11 81 94 然后和数组中的下一个数字进行比较,也就是99,进行比较发现99大于11,所以99和11一个梯队,并从内存中清除11,并将11加入第一梯队,并将99加进去……

思路:有用优先队列的,有用vector的,有用set的。不过vector+sort肯定会超时,当然也可以对sort优化,这道题我最后一个测试点没过55555。留下菜比的眼泪,最好还是用自带排序的容器吧,避免超时,肯定有大佬的思路比我好的多的。
大概就是用两个set来存放当前内存的第一梯队元素和第二梯队元素,然后第一梯队元素输出完了之后就令第二梯队为新的第一梯队,再将第二梯队clear()又能开始存储新的第二梯队元素了。我的思路,最后一个测试点没过。

#include <cstdio> 
#include <queue> 
#include <vector> 
using namespace std; 
int main() { 
	int N, M; scanf("%d%d", &N, &M); 
	vector<int> arr(N); 
	for (int i = 0; i < N; i++) scanf("%d", &arr[i]); 
	priority_queue<int, vector<int>, greater<int>> q; 
	vector<int> v,line; 
	int index = 0,count=0, last; 
	for (; index < M; index++) q.push(arr[index]); 
	while (count != N) { 
		last = q.top(); 
		line.push_back(last); 
		q.pop(); count++; 
		if (index < N) { 
			if (arr[index] > last) q.push(arr[index++]); 
			else v.push_back(arr[index++]); 
		} 
		if (q.empty()) { 
			for (int i = 0; i < line.size(); i++) { 
				if (i != 0) printf(" "); 
				printf("%d", line[i]); 
			} 
			printf("\n"); line.clear(); 
			for (int i = 0; i < v.size(); i++) 
				q.push(v[i]); 
			v.clear(); 
		} 
	} 
}

总场考试下来就90来分,100多名好像,我太废物了太废物了,第二题 different 和 difference 傻傻分不清,浪费了1个小时。 一直在想not be duplicated 和 different 不是同一个意思吗,后来才发现是the difference of two nums previously…。

点个赞,点个赞

  • 15
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值