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

Posted 花落,莫相离

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT(甲级)2019年春季考试题解相关的知识,希望对你有一定的参考价值。

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 http://mathworld.wolfram.com/SexyPrimes.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 (≤10^​8​​).

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 <bits/stdc++.h>
using namespace std;

bool is_prime(int a){
    if(a <= 1)  return false;
    int sqr = (int)sqrt(1.0*a);
    for(int i = 2; i <= sqr; i++){
        if(a % i == 0)  return false;
    }
    return true;
}

int judge(int a){
    if(!is_prime(a))    return 0;
    else{
        if(is_prime(a-6))   return a-6;
        else if(is_prime(a+6))  return a+6;
    }
    return 0;
}

int main(){
    int n;
    scanf("%d", &n);
    int num = judge(n);
    if(!num){
        printf("No\\n");
        while(!judge(n))    n++;
        printf("%d\\n", n);
    }else{
        printf("Yes\\n%d\\n", num);
    }
    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 (≤10​^5​​). 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 (≤10^​5​​). 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 <bits/stdc++.h>
using namespace std;

unordered_map<string, int> alu;

int main(){
    int n, m;
    int oldest = 20200804;
    int c_oldest = 20200804;
    string people, old, c_old;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> people;
        alu[people] = 1;
    }
    int cot = 0;
    scanf("%d", &m);
    for(int i = 0; i < m; i++){
        cin >> people;
        if(stoi(people.substr(6,8)) < oldest){
            old = people;
            oldest = stoi(people.substr(6,8));
        }
        if(alu[people]){
            cot++;
            if(stoi(people.substr(6,8)) < c_oldest){
                c_old = people;
                c_oldest = stoi(people.substr(6,8));
            }
        }
    }
    printf("%d\\n", cot);
    if(!cot)    cout << old << endl;    //读题读清楚,是来的客人年龄最大的
    else    cout << c_old << endl;
    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 shortphone 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 (≤10​^3​​, the number of different phone numbers), and M (≤10^​5​​, 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

思路分析:
最后的note竟然没看到。。

参考代码:

#include <bits/stdc++.h>
using namespace std;\\

const int maxn = 1010;

int k, n, m;
int is_gang[maxn];
int record[maxn][maxn];  //记录每个人的通话记录
int dis[maxn][maxn];
bool vis[maxn] = {false};
vector<int> gang;

void DFS(int s){
    vis[s] = true;
    gang.push_back(s);
    for(int i = 1; i <= n; i++){
        if(!vis[i] && dis[s][i] && is_gang[i])  DFS(i);
    }
}

int main(){
    int caller, receiver, duration;
    fill(dis[0], dis[0]+maxn*maxn, 0);
    scanf("%d %d %d", &k, &n, &m);
    fill(record[0], record[0]+maxn*maxn, 0);
    for(int i = 0; i < m; i++){
        scanf("%d %d %d", &caller, &receiver, &duration);
        record[caller][receiver] += duration;
    }
    int num = 0;
    for(int i = 1; i <= n; i++){
        int call = 0, call_back = 0;
        for(int j = 1; j <= n; j++){
            if(0 < record[i][j] && record[j][i] > 0)    dis[i][j] = dis[j][i] = 1;
            if(0 < record[i][j] && record[i][j] <= 5){//此处两个条件不能放在一起判断
                call++;
                if(record[j][i])  call_back++;
            }
        }
        if(call > k && call_back <= call*0.2){//阈值是指短电话个数
            is_gang[i] = 1;
            num++;
        }
    }
    if(!num)  printf("None\\n");
    else{
        for(int i = 1; i <= n; i++){
            if(!vis[i] && is_gang[i]){
                gang.clear();
                DFS(i);
                sort(gang.begin(), gang.end());
                for(int i = 0; i < gang.size(); i++)
                printf("%d%s", gang[i], i == gang.size()-1 ? "\\n" : " ");
            }
        }
    }
    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 10​^3​​ 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 <bits/stdc++.h>
using namespace std;

const int maxn = 35;

struct node{
    int data;
    node *left, *right;
};

int n, flag = 1;
int post[maxn], in[maxn];
int level[maxn], parent[maxn], l[maxn], r[maxn];

node* build(int postL, int postR, int inL, int inR){
    if(postL > postR)   return NULL;
    node* root = new node;
    int pos;
    for(int i = inL; i <= inR; i++){
        if(in[i] == post[postR]){
            pos = i;
            break;
        }
    }
    root->data = post[postR];
    root->left = build(postL, postL+pos-1-inL, inL, pos-1);
    root->right = build(postR-1-(inR-pos-1), postR-1, pos+1, inR);
    return root;
}

void DFS(node* root){
    if(root == NULL)    return;
    int num = 0;
    if(root->left != NULL){
        num++;
        level[root->left->data] = level[root->data]+1;
        parent[root->left->data] = root->data;
        l[root->data] = root->left->data;
        DFS(root->left);
    }
    if(root->right != NULL){
        num++;
        level[root->right->data] = level[root->data]+1;
        parent[root->right->data] = root->data;
        r[root->data] = root->right->data;
        DFS(root->right);
    }
    if(num == 1)    flag = 0;
}

int main(){
    int m;
    string check;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)  scanf("%d", &post[i]);
    for(int i = 0; i < n; i++)  scanf("%d", &in[i]);
    node* root = new node;
    root = build(0, n-1, 0, n-1);
    level[root->data] = 1;
    DFS(root);
    scanf("%d\\n", &m);
    for(int i = 0; i < m; i++){
        int A, B;
        getline(cin, check);
        if(check.find("root") != string::npos){
            sscanf(check.c_str(), "%d is the root", &A);
            if(A == root->data) printf("Yes\\n");
            else printf("No\\n");
        }else if(check.find("siblings") != string::npos){
            sscanf(check.c_str(), "%d and %d are siblings", &A, &B);
            if(parent[A] == parent[B])  printf("Yes\\n");
            else    printf("No\\n");
        }else if(check.find("parent") != string::npos){
            sscanf(check.c_str(), "%d is the parent of %d", &A, &B);
            if(parent[B] == A)  printf("Yes\\n");
            else    printf("No\\n");
        }else if(check.find("left") != string::npos){
            sscanf(check.c_str(), "%d is the left child of %d", &A, &B);
            if(l[B] == A)   printf("Yes\\n");
            else    printf("No\\n");
        }else if(check.find("right") != string::npos){
            sscanf(check.c_str(), "%d is the right child of %d", &A, &B);
            if(r[B] == A)   printf("Yes\\n");
            else    printf("No\\n");
        }else if(check.find("level") != string::npos){
            sscanf(check.c_str(), "%d and %d are on the same level", &A, &B);
            if(level[A] == level[B])    printf("Yes\\n");
            else    printf("No\\n");
        }else{
            if(flag) printf("Yes\\n");
            else    printf("No\\n");
        }
    }
    return 0;
}

以上是关于PAT(甲级)2019年春季考试题解的主要内容,如果未能解决你的问题,请参考以下文章

PAT(甲级)2019年春季考试 7-4 Structure of a Binary Tree

PAT(甲级)2019年春季考试 7-3 Telefraud Detection

PAT(甲级)2017年春季考试

PAT(甲级)2020年春季考试 7-2 The Judger

PAT(甲级)2021年春季考试 7-4 Recycling of Shared Bicycles

PAT(甲级)2020年春季考试 7-4 Replacement Selection