PAT(甲级)2019年春季考试题解

编程入门 行业动态 更新时间:2024-10-24 08:29:32

PAT(甲级)2019年春季考试<a href=https://www.elefans.com/category/jswz/34/1769599.html style=题解"/>

PAT(甲级)2019年春季考试题解

本来想用PAT抵浙大的机试成绩,结果忘记时间错过了报名春季的考试(其实是只刷了十几题心里没底想晚点再报名,后来就忘记了)…然后看题库发现现在PTA平台不会同步更新最新的考试题目了,要去教育超市花钱买(稍微有点坑),不过好处是花了钱我才能更加重视…于是在今天–在考前的一周,下午一点半准时模拟了下周的考试,所幸练习的结果不错,要是考试也能有这样的发挥就好了。

7-1 Sexy Primes (20 分)

Sexy primes are pairs of primes of the form (p, p+6), so-named since “sex” is the Latin word for “six”. (Quoted from .html)

Now given an integer, you are supposed to tell if it is a sexy prime.

Input Specification:
Each input file contains one test case. Each case gives a positive integer N (≤ 1 0 8 10^8 108​​ ).

Output Specification:
For each case, print in a line Yes if N is a sexy prime, then print in the next line the other sexy prime paired with N (if the answer is not unique, output the smaller number). Or if N is not a sexy prime, print No instead, then print in the next line the smallest sexy prime which is larger than N.

Sample Input 1:
47
Sample Output 1:
Yes
41
Sample Input 2:
21
Sample Output 2:
No
23

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<string>
#include<cstring>
#include<unordered_map>
#include<unordered_set>
using namespace std;
const int maxn=100;
const int INF=0x3f3f3f3f;
bool isprime(long long a) {if(a<=1) return false;for(long long i=2; i*i<=a; i++) {if(a%i==0) return false;}return true;
}
int main() {int n;scanf("%d",&n);if(isprime(n)) {if(isprime(n-6)) printf("Yes\n%d",n-6);else if(isprime(n+6)) printf("Yes\n%d",n+6);else {for(int i=n+1;; i++) {if(isprime(i) && (isprime(i+6) || isprime(i-6))) {printf("No\n%d",i);break;}}}} else {for(int i=n+1;; i++) {if(isprime(i) && (isprime(i+6) || isprime(i-6))) {printf("No\n%d",i);break;}}}return 0;
}

7-2 Anniversary (25 分)

Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebration, the alumni association (校友会) has gathered the ID’s of all her alumni. Now your job is to write a program to count the number of alumni among all the people who come to the celebration.

Input Specification:
Each input file contains one test case. For each case, the first part is about the information of all the alumni. Given in the first line is a positive integer N (≤ 1 0 5 10^5 105​​ ). Then N lines follow, each contains an ID number of an alumnus. An ID number is a string of 18 digits or the letter X. It is guaranteed that all the ID’s are distinct.

The next part gives the information of all the people who come to the celebration. Again given in the first line is a positive integer M (≤ 1 0 5 10^5 105​​ ).Then M lines follow, each contains an ID number of a guest. It is guaranteed that all the ID’s are distinct.

Output Specification:
First print in a line the number of alumni among all the people who come to the celebration. Then in the second line, print the ID of the oldest alumnus – notice that the 7th - 14th digits of the ID gives one’s birth date. If no alumnus comes, output the ID of the oldest guest instead. It is guaranteed that such an alumnus or guest is unique.

Sample Input:
5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042
Sample Output:
3
150702193604190912

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<string>
#include<cstring>
#include<unordered_map>
#include<unordered_set>
using namespace std;
const int maxn=100;
const int INF=0x3f3f3f3f;int main() {int n,m,sum=0;scanf("%d",&n);string id,oldest,guest,sub;unordered_map<string,int> alumni;for(int i=0; i<n; i++) {cin>>id;alumni[id]++;}scanf("%d",&m);for(int i=0; i<m; i++) {cin>>id;sub=id.substr(6,8);if(i==0) guest=id;if(sum==0 && sub<guest.substr(6,8)) guest=id;if(alumni[id]!=0) {if(sum==0) oldest=id;if(sub<oldest.substr(6,8)) oldest=id;sum++;}}printf("%d\n",sum);if(sum==0) printf("%s\n",guest.c_str());else printf("%s\n",oldest.c_str());return 0;
}

7-3 Telefraud Detection (25 分)

Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone call records.

A person must be detected as a suspect if he/she makes more than K short phone calls to different people everyday, but no more than 20% of these people would call back. And more, if two suspects are calling each other, we say they might belong to the same gang. A makes a short phone call to B means that the total duration of the calls from A to B is no more than 5 minutes.

Input Specification:
Each input file contains one test case. For each case, the first line gives 3 positive integers K (≤500, the threshold(阈值) of the amount of short phone calls), N (≤ 1 0 3 10^3 103​​ )​​ , the number of different phone numbers), and M (≤ 1 0 5 10^5 105​​ )​​ , the number of phone call records). Then M lines of one day’s records are given, each in the format:

caller receiver duration
where caller and receiver are numbered from 1 to N, and duration is no more than 1440 minutes in a day.

Output Specification:
Print in each line all the detected suspects in a gang, in ascending order of their numbers. The gangs are printed in ascending order of their first members. 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.

If no one is detected, output None instead.

Sample Input 1:
5 15 31
1 4 2
1 5 2
1 5 4
1 7 5
1 8 3
1 9 1
1 6 5
1 15 2
1 15 5
3 2 2
3 5 15
3 13 1
3 12 1
3 14 1
3 10 2
3 11 5
5 2 1
5 3 10
5 1 1
5 7 2
5 6 1
5 13 4
5 15 1
11 10 5
12 14 1
6 1 1
6 9 2
6 10 5
6 11 2
6 12 1
6 13 1
Sample Output 1:
3 5
6
Note: In sample 1, although 1 had 9 records, but there were 7 distinct receivers, among which 5 and 15 both had conversations lasted more than 5 minutes in total. Hence 1 had made 5 short phone calls and didn’t exceed the threshold 5, and therefore is not a suspect.

Sample Input 2:
5 7 8
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
2 1 1
3 1 1
Sample Output 2:
None

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<string>
#include<cstring>
#include<unordered_map>
#include<unordered_set>
using namespace std;
const int maxn=1005;
const int INF=0x3f3f3f3f;
int G[maxn][maxn];
bool vis[maxn];
int main() {int k,n,m,a,b,t;scanf("%d%d%d",&k,&n,&m);for(int i=0; i<m; i++) {scanf("%d%d%d",&a,&b,&t);G[a][b]+=t;}vector<int> v;for(int i=1; i<=n; i++) {int cnt=0,back=0;for(int j=1; j<=n; j++) {if(G[i][j]!=0 && G[i][j]<=5) {cnt++;if(G[j][i]!=0) back++;}}double per=back*1.0/cnt;if(cnt>k && per<=0.2) v.push_back(i);}for(int i=0; i<v.size(); i++) {int cur=v[i];if(!vis[cur]) {printf("%d",cur);vis[cur]=true;queue<int> other;vector<int> gang;for(int j=i+1; j<v.size(); j++) {int tmp=v[j];if(!vis[tmp] && G[cur][tmp]!=0 && G[tmp][cur]!=0) {vis[tmp]=true;other.push(tmp);gang.push_back(tmp);}}while(!other.empty()) {int first=other.front();other.pop();for(int j=0; j<v.size(); j++) {int tmp=v[j];if(!vis[tmp] && G[first][tmp]!=0 && G[tmp][first]!=0) {vis[tmp]=true;other.push(tmp);gang.push_back(tmp);}}}sort(gang.begin(),gang.end());for(int j=0; j<gang.size(); j++) {printf(" %d",gang[j]);}printf("\n");}}if(v.size()==0) puts("None");return 0;
}

7-4 Structure of a Binary Tree (30 分)

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined.

Now given a sequence of statements about the structure of the resulting tree, you are supposed to tell if they are correct or not. A statment is one of the following:

A is the root
A and B are siblings
A is the parent of B
A is the left child of B
A is the right child of B
A and B are on the same level
It is a full tree
Note:
Two nodes are on the same level, means that they have the same depth.
A full binary tree is a tree in which every node other than the leaves has two children.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are no more than 1 0 3 10^3 103 and are separated by a space.

Then another positive integer M (≤30) is given, followed by M lines of statements. It is guaranteed that both A and B in the statements are in the tree.

Output Specification:
For each statement, print in a line Yes if it is correct, or No if not.

Sample Input:
9
16 7 11 32 28 2 23 8 15
16 23 7 32 11 2 28 15 8
7
15 is the root
8 and 2 are siblings
32 is the parent of 11
23 is the left child of 16
28 is the right child of 2
7 and 11 are on the same level
It is a full tree
Sample Output:
Yes
No
Yes
No
Yes
Yes
Yes

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<string>
#include<cstring>
#include<unordered_map>
#include<unordered_set>
using namespace std;
const int maxn=1005;
const int INF=0x3f3f3f3f;
unordered_map<int,int> level,parents;
struct node {int data;int layer;node *lchild,*rchild;
};
vector<int> post,in;
node* newNode(int data,int layer) {node* root=new node;root->data=data;root->layer=layer;level[data]=layer;root->lchild=root->rchild=NULL;return root;
}
bool flag=true;
node* create(int parent,int postLeft,int postRight,int inLeft,int inRight,int layer) {if(postLeft>postRight) return NULL;node* root=newNode(post[postRight],layer);parents[root->data]=parent;int index=inLeft;while(in[index]!=root->data) index++;int numLeft=index-inLeft;root->lchild=create(root->data,postLeft,postLeft+numLeft-1,inLeft,index-1,layer+1);root->rchild=create(root->data,postLeft+numLeft,postRight-1,index+1,inRight,layer+1);//如果有叶子,就必须有两片叶子if((root->lchild || root->rchild ) && (!root->lchild || !root->rchild)) flag=false;return root;
}
int main() {int n,m;unordered_map<int,int> ppos,ipos;scanf("%d",&n);post.resize(n);in.resize(n);for(int i=0; i<n; i++) {scanf("%d",&post[i]);ppos[post[i]]=i;}for(int i=0; i<n; i++) {scanf("%d",&in[i]);ipos[in[i]]=i;}node* root = create(n-1,0,n-1,0,n-1,0);scanf("%d\n",&m);string ask;for(int i=0; i<m; i++) {getline(cin,ask);int num1=0,num2=0;if(ask.find("root")!=string::npos) {sscanf(ask.c_str(),"%d is the root",&num1);if(ppos[num1]==n-1) puts("Yes");else puts("No");} else if(ask.find("siblings")!=string::npos) {sscanf(ask.c_str(),"%d and %d are siblings",&num1,&num2);if(level[num1]==level[num2] && parents[num1]==parents[num2]) puts("Yes");else puts("No");} else if(ask.find("parent")!=string::npos) {sscanf(ask.c_str(),"%d is the parent of %d",&num1,&num2);if(parents[num2]==num1) puts("Yes");else puts("No");} else if(ask.find("left")!=string::npos) {sscanf(ask.c_str(),"%d is the left child of %d",&num1,&num2);if(parents[num1]==num2 && ipos[num1]<ipos[num2]) puts("Yes");else puts("No");} else if(ask.find("right")!=string::npos) {sscanf(ask.c_str(),"%d is the right child of %d",&num1,&num2);if(parents[num1]==num2 && ipos[num1]>ipos[num2]) puts("Yes");else puts("No");} else if(ask.find("same")!=string::npos) {sscanf(ask.c_str(),"%d and %d are on the same level",&num1,&num2);if(level[num1]==level[num2]) puts("Yes");else puts("No");} else if(ask.find("full")!=string::npos) {if(flag) puts("Yes");else puts("No");}}return 0;
}

更多推荐

PAT(甲级)2019年春季考试题解

本文发布于:2024-03-10 14:41:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1728250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:题解   春季   考试   PAT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!