PAT(甲级)2020年春季考试

PAT(甲级)2020年春季考试

自己测试了一下,两个小时97分,第二道题出了一点问题,检查不出来,就懒得检查了.

7-1 Prime Day (20 分)

The above picture is from Sina Weibo, showing May 23rd, 2019 as a very cool “Prime Day”. That is, not only that the corresponding number of the date 20190523 is a prime, but all its sub-strings ended at the last digit 3 are prime numbers.

Now your job is to tell if a given date is a Prime Day.

Input Specification:

Each input file contains one test case. For each case, a date between January 1st, 0001 and December 31st, 9999 is given, in the format yyyymmdd.

Output Specification:

For each given date, output in the decreasing order of the length of the substrings, each occupies a line. In each line, print the string first, followed by a space, then Yes if it is a prime number, or No if not. If this date is a Prime Day, print in the last line All Prime!.

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 a){
	if(a<=1) return false;
	for(int i=2;i<=sqrt(a);++i){
		if(a%i==0) return false;
	}
	return true;
}
int main(){
	string num;
	cin>>num;
	int cnt=0;
	for(int i=0;i<num.length();++i){
		string sub=num.substr(i,num.length()-i);
		if(isPrime(stoi(sub))){
			cout<<sub<<' '<<"Yes\n";
		}
		else cout<<sub<<' '<<"No\n",++cnt;
	}
	if(!cnt) cout<<"All Prime!";
} 

7-2 The Judger (25 分)

A game of numbers has the following rules: at the beginning, two distinct positive integers are given by the judge. Then each player in turn must give a number to the judge. The number must be the difference of two numbers that are previously given, and must not be duplicated to any of the existed numbers. The game will run for several rounds. The one who gives a duplicate number or even a wrong number will be kicked out.

Your job is to write a judger program to judge the players’ numbers and to determine the final winners.

Input Specification:

Each input file contains one test case. For each case, the first line gives two distinct positive integers to begin with. Both numbers are in [1,105].

In the second line, two numbers are given: N (2≤N≤10), the number of players, and M (2≤M≤103), the number of rounds.

Then N lines follow, each contains M positive integers. The i-th line corresponds to the i-th player (i=1,⋯,N). The game is to start from the 1st player giving his/her 1st number, followed by everybody else giving their 1st numbers in the 1st round; then everyone give their 2nd numbers in the 2nd round, and so on so forth.

Output Specification:

If the i-th player is kicked out in the k-th round, print in a line Round #k: i is out.. The rest of the numbers given by the one who is out of the game will be ignored. If more than one player is out in the same round, print them in increasing order of their indices. When the game is over, print in the last line Winner(s): W1 W2 ... Wn, where W1 ... Wn are the indices of the winners in increasing order. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line. If there is no winner, print No winner. instead.

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.

题解

我只得了22分这题,我也不知道为啥错,感觉没啥问题,有没有大哥解答一下,,,

我的思路就是每次对集合里的元素看看找不找得到(it+num)在里面。。。

代码

没AC的代码

#include<bits/stdc++.h>
using namespace std;
vector<int> vec[50];
bool isKick[50];
int main(){
	int a,b,n,m;
	cin>>a>>b>>n>>m;
	unordered_set<int> s;
	s.insert(a),s.insert(b);
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			cin>>a;
			vec[i].push_back(a);
		}
	}
	for(int i=0;i<m;++i){
		for(int j=1;j<=n;++j){
			if(isKick[j]==0){
				int judge=0;
				int num=vec[j][i];
				if(s.find(num)!=s.end()) isKick[j]=1;
				else{
					for(auto it : s){
						if(s.find(it+num)!=s.end()){
							judge=1;
							break;
						}
					}
					if(judge==0) isKick[j]=1;
					s.insert(num);
				}
				if(isKick[j]==1) printf("Round #%d: %d is out.\n",i+1,j);
			}
		}
	}	
	int existW=0;
	for(int i=1;i<=n;++i){
		if(isKick[i]==0){
			existW=1;
			break;
		}
	}
	if(!existW) cout<<"No winner.";
	else{
		cout<<"Winner(s):";
		for(int i=1;i<=n;++i){
			if(isKick[i]==0){
				cout<<' '<<i;
			}
		}
	}
} 

**7-3 Safari Park **

A safari park(野生动物园)has K species of animals, and is divided into N regions. The managers hope to spread the animals to all the regions, but not the same animals in the two neighboring regions. Of course, they also realize that this is an NP complete problem, you are not expected to solve it. Instead, they have designed several distribution plans. Your job is to write a program to help them tell if a plan is feasible.

Input Specification:

Each input file contains one test case. For each case, the first line gives 3 integers: N (0<N≤500), the number of regions; R (≥0), the number of neighboring relations, and K (0<KN), the number of species of animals. The regions and the species are both indexed from 1 to N.

Then R lines follow, each gives the indices of a pair of neighboring regions, separated by a space.

Finally there is a positive M (≤20) followed by M lines of distribution plans. Each plan gives N indices of species in a line (the i-th index is the animal in the i-th rigion), separated by spaces. It is guaranteed that any pair of neighboring regions must be different, and there is no duplicated neighboring relations.

Output Specification:

For each plan, print in a line Yes if no animals in the two neighboring regions are the same, or No otherwise. However, if the number of species given in a plan is not K, you must print Error: Too many species. or Error: Too few species. according to the case.

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.

题解

普通的建图题目,比较简单

代码

#include<bits/stdc++.h>
using namespace std;
int G[1000][1000];
int species[1000];
int main(){
	int a,b,n,r,k,m;
	cin>>n>>r>>k;
	for(int i=0;i<r;++i){
		cin>>a>>b;
		G[a][b]=G[b][a]=1;
	}
	cin>>m;
	for(int i=0;i<m;++i){
		int cnt=0;
		unordered_set<int> s;
		for(int j=1;j<=n;++j){
			
			cin>>species[j];
			if(s.find(species[j])==s.end()) ++cnt;
			s.insert(species[j]);
		}
		if(cnt!=k) {
			if(cnt>k) cout<<"Error: Too many species.\n";
			else cout<<"Error: Too few species.\n";
		}
		else{
			int flag=0;
			for(int u=1;u<=n;++u){
				for(int v=u+1;v<=n;++v){
					if(G[u][v]==1 && species[u]==species[v]){
						flag=1;
						break;
					}
					if(flag==1) break;
				}
			}
			if(flag) cout<<"No\n";
			else cout<<"Yes\n";
		}
	}
	
} 

7-4 Replacement Selection

When the input is much too large to fit into memory, we have to do external sorting instead of internal sorting. One of the key steps in external sorting is to generate sets of sorted records (also called runs) with limited internal memory. The simplest method is to read as many records as possible into the memory, and sort them internally, then write the resulting run back to some tape. The size of each run is the same as the capacity of the internal memory.

Replacement Selection sorting algorithm was described in 1965 by Donald Knuth. Notice that as soon as the first record is written to an output tape, the memory it used becomes available for another record. Assume that we are sorting in ascending order, if the next record is not smaller than the record we have just output, then it can be included in the run.

For example, suppose that we have a set of input { 81, 94, 11, 96, 12, 99, 35 }, and our memory can sort 3 records only. By the simplest method we will obtain three runs: { 11, 81, 94 }, { 12, 96, 99 } and { 35 }. According to the replacement selection algorithm, we would read and sort the first 3 records { 81, 94, 11 } and output 11 as the smallest one. Then one space is available so 96 is read in and will join the first run since it is larger than 11. Now we have { 81, 94, 96 }. After 81 is out, 12 comes in but it must belong to the next run since it is smaller than 81. Hence we have { 94, 96, 12 } where 12 will stay since it belongs to the next run. When 94 is out and 99 is in, since 99 is larger than 94, it must belong to the first run. Eventually we will obtain two runs: the first one contains { 11, 81, 94, 96, 99 } and the second one contains { 12, 35 }.

Your job is to implement this replacement selection algorithm.

Input Specification:

Each input file contains several test cases. The first line gives two positive integers N (≤105) and M (<N/2), which are the total number of records to be sorted, and the capacity of the internal memory. Then N numbers are given in the next line, all in the range of int. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in each line a run (in ascending order) generated by the replacement selection algorithm. All the numbers in a line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

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

题解

我用的优先队列做的,其实挺简单的,过程一下子就模拟出来了,但后面忘记了第一个队列还会有存在的数,printf大法检查了好久。

就是维护两个优先队列,第一个就是要在内存里跑的,第二个优先队列的话就是在外面的,也就是说题目本来是说假如小于 o u t p u t out_put output的话要

插进队列里面并且最后输出,但是我偏不,我把它插入另外一个队列里面,并且–x,表示第一个队列的位置-1。大概就是这个意思。

代码

#include<bits/stdc++.h>
#include<queue>
using namespace std;
int main(){
	long long  n,k,nums[200000];
	cin>>n>>k;
	for(int i=0;i<n;++i) scanf("%lld",&nums[i]);//cin>>nums[i]; 
	priority_queue<long long ,vector<long long>,greater<long long > > que;
	priority_queue<long long ,vector<long long >,greater<long long> > tmp;
	long long out_put=-10000000000;
	int x=k;
	int flag=0;
	for(int i=0;i<n;++i){
		if(que.size()<x && nums[i]>=out_put) que.push(nums[i]);
		else if(que.size()<x && nums[i]<out_put){
			tmp.push(nums[i]),x-=1;
			if(x==0){
				cout<<endl;
				flag=0;
				x=k;
				que=tmp;
				priority_queue<long long ,vector<long long >,greater<long long> > tmp1;
				tmp=tmp1;
				//while(!tmp.empty()) tmp.pop();
			}
		}
		if(que.size()==x){
			if(flag) cout<<' ';
			cout<<que.top();
			flag=1;
			out_put=que.top();
			que.pop();
		}
		
	}
	while(!que.empty()){
		cout<<' '<<que.top();
		que.pop();
	}
	cout<<endl;
	flag=0;
	while(!tmp.empty()){
		if(flag) cout<<' ';
		cout<<tmp.top();
		flag=1;
		tmp.pop(); 
	}
	
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值