PAT(甲级)2019年春季考试
开始时间1/1/2016, 08:00:00
结束时间1/18/2038, 08:00:00
答题时长180分钟
考生charjindev
得分100
总分100

编程题总分:100得分:100
7-1
Sexy Primes

Sexy primes are pairs of primes of the form (pp, p+6p+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 NN (108\le 10^8).

Output Specification:

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

Sample Input 1:

47

Sample Output 1:

Yes
41

Sample Input 2:

21

Sample Output 2:

No
23
编译器
GXX
代码
#include <cstdio>
bool isprime(int x) {
    if (x <= 1) return false;
    for (int i = 2; i * i <= x; i++)
        if (x % i == 0) return false;
    return true;
}
int main() {
    int n;
    scanf("%d", &n);
    if (isprime(n) && isprime(n - 6)) printf("Yes\n%d", n - 6);
    else if (isprime(n) && isprime(n + 6)) printf("Yes\n%d", n + 6);
    else {
        while (true) {
            n++;
            if (isprime(n) && (isprime(n - 6) || isprime(n + 6))) break;
        }
        printf("No\n%d", n);
    }
    return 0;
}
评测结果:答案正确(20 分)
测试点结果得分耗时内存
0答案正确63 ms352 KB
1答案正确64 ms392 KB
2答案正确24 ms384 KB
3答案正确23 ms424 KB
4答案正确34 ms384 KB
5答案正确12 ms356 KB
a.cpp: In function ‘int main()’:
a.cpp:17:24: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
     if (ans = issexy(n)) {
                        ^
a.cpp:16:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
                    ^
7-2
Anniversary

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 NN (105\le 10^5). Then NN 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 MM (105\le 10^5). Then MM 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
编译器
GXX
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, m, cnt = 0;
    string oldestAlumni = "xxxxxx99999999", oldestGuest = "xxxxxx99999999", id;
    scanf("%d", &n);
    map<string, bool> isalumni;
    for (int i = 0; i < n; i++) {
        cin >> id;
        isalumni[id] = true;
    }
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        cin >> id;
        if (id.substr(6, 8) < oldestGuest.substr(6)) oldestGuest = id;
        if (isalumni[id]) {
            if (id.substr(6, 8) < oldestAlumni.substr(6)) oldestAlumni = id;
            cnt++;
        }
    }
    printf("%d\n%s", cnt, cnt > 0 ? oldestAlumni.c_str() : oldestGuest.c_str());
    return 0;
}
评测结果:答案正确(25 分)
测试点结果得分耗时内存
0答案正确134 ms420 KB
1答案正确22 ms384 KB
2答案正确13 ms384 KB
3答案正确4245 ms22272 KB
4答案正确5223 ms22144 KB
a.cpp: In function ‘int main()’:
a.cpp:5:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
                    ^
a.cpp:12:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &m);
                    ^
7-3
Telefraud Detection

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 KK 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. AA makes a short phone call to BB means that the total duration of the calls from AA to BB 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 KK (500\le 500, the threshold(阈值) of the amount of short phone calls), NN (103\le 10^3, the number of different phone numbers), and MM (105\le 10^5, the number of phone call records). Then MM lines of one day's records are given, each in the format:

caller receiver duration

where caller and receiver are numbered from 1 to NN, 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
编译器
GXX
代码
#include <bits/stdc++.h>
using namespace std;
int father[1010];
int findFather(int x) {
    return x == father[x] ? x : father[x] = findFather(father[x]);
}
void uni(int a, int b) {
    int faA = findFather(a);
    int faB = findFather(b);
    if (faA < faB) father[faB] = faA;
    if (faA > faB) father[faA] = faB;
}
int main() {
    for (int i = 0; i < 1010; i++) father[i] = i;
    int k, n, m, a, b, time;
    scanf("%d%d%d", &k, &n, &m);
    vector<map<int, int> > record(n + 1);
    for (int i = 0; i < m; i++) {
        scanf("%d%d%d", &a, &b, &time);
        record[a][b] += time;
    }
    vector<int> suspect;
    for (int i = 1; i <= n; i++) {
        if (record[i].size() <= k) continue;
        int callto = 0, callback = 0;
        for (auto it : record[i]) {
            if (it.second <= 5) {
                callto++;
                if (record[it.first].find(i) != record[it.first].end()) callback++;
            }
        }
        if (callto > k && callback * 1.0 / callto <= 0.2) suspect.push_back(i);
    }
    if (suspect.size() == 0) {
        printf("None\n");
        return 0;
    }
    for (int i = 0; i < suspect.size() - 1; i++) {
        for (int j = i + 1; j < suspect.size(); j++) {
            a = suspect[i]; b = suspect[j];
            if (record[a][b] > 0 && record[b][a] > 0) uni(a, b);
        }
    }
    map<int, vector<int> > mp;
    for (auto it : suspect) mp[findFather(it)].push_back(it);
    for (auto it : mp) {
        vector<int> gang = move(it.second);
        sort(gang.begin(), gang.end());
        for (int i = 0; i < gang.size(); i++) {
            if (i != 0) printf(" ");
            printf("%d", gang[i]);
        }
        printf("\n");
    }
    return 0;
}
评测结果:答案正确(25 分)
测试点结果得分耗时内存
0答案正确134 ms424 KB
1答案正确14 ms548 KB
2答案正确64 ms344 KB
3答案正确14 ms384 KB
4答案正确479 ms4608 KB
a.cpp: In function ‘int main()’:
a.cpp:27:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (record[i].size() <= k) continue;
             ~~~~~~~~~~~~~~~~~^~~~
a.cpp:43:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < suspect.size() - 1; i++) {
                     ~~^~~~~~~~~~~~~~~~~~~~
a.cpp:44:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = i + 1; j < suspect.size(); j++) {
                             ~~^~~~~~~~~~~~~~~~
a.cpp:59:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < gang.size(); i++) {
                         ~~^~~~~~~~~~~~~
a.cpp:19:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &k, &n, &m);
                                ^
a.cpp:22:39: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d", &a, &b, &time);
                                       ^
7-4
Structure of a Binary Tree

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 NN (30\le 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 10310^3 and are separated by a space.

Then another positive integer MM (30\le 30) is given, followed by MM 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
编译器
GXX
代码
#include <bits/stdc++.h>
using namespace std;
vector<int> post, in;
int tree[1010], pos[1010], level[1010], len = -1;
int create(int postl, int postr, int inl, int inr, int i, int depth) {
    if (postl > postr) return 0;
    len = max(len, i);
    level[post[postr]] = depth;
    tree[i] = post[postr];
    int k = inl;
    while (k <= inr && in[k] != post[postr]) k++;
    int numLeft = k - inl;
    tree[i * 2] = create(postl, postl + numLeft - 1, inl, k - 1, i * 2, depth + 1);
    tree[i * 2 + 1] = create(postl + numLeft, postr - 1, k + 1, inr, i * 2 + 1, depth + 1);
    return post[postr];
}
int main() {
    int n, m;
    scanf("%d", &n);
    post.resize(n);
    in.resize(n);
    for (int i = 0; i < n; i++) scanf("%d", &post[i]);
    for (int i = 0; i < n; i++) scanf("%d", &in[i]);
    create(0, n - 1, 0, n - 1, 1, 1);
    bool isfull = true;
    for (int i = 1; i <= len; i++) {
        if (tree[i] == 0) continue;
        pos[tree[i]] = i;
        if ((tree[i * 2] != 0 || tree[i * 2 + 1] != 0) &&
            (tree[i * 2] * tree[i * 2 + 1] == 0)) {
            isfull = false;
        }
    }
    scanf("%d", &m);
    getchar();
    while (m--) {
        string s;
        bool ans;
        getline(cin, s);
        if (s[0] == 'I') ans = isfull;
        else {
            int a = stoi(s.substr(0, s.find_first_of(' ')));
            if (s[s.length() - 1] == 't') ans = pos[a] == 1;
            else if (isdigit(s[s.length() - 1])) {
                int b = stoi(s.substr(s.find_last_of(' ') + 1));
                if (s.find("pa") != string::npos) ans = pos[a] == pos[b] / 2;
                else if (s.find("le") != string::npos) ans = pos[a] == pos[b] * 2;
                else ans = pos[a] == pos[b] * 2 + 1;
            } else {
                s.erase(0, s.find_first_of(' '));
                while (!isdigit(s[0])) s.erase(s.begin());
                int b = stoi(s.substr(0, s.find_first_of(' ')));
                if (s[s.length() - 1] == 's') ans = pos[a] / 2 == pos[b] / 2;
                else ans = level[a] == level[b];
            }
        }
        printf("%s\n", ans ? "Yes" : "No");
    }
    return 0;
}
评测结果:答案正确(30 分)
测试点结果得分耗时内存
0答案正确153 ms296 KB
1答案正确54 ms384 KB
2答案正确54 ms424 KB
3答案正确25 ms424 KB
4答案正确34 ms384 KB
a.cpp: In function ‘int main()’:
a.cpp:25:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
                    ^
a.cpp:26:54: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 0; i < n; i++) scanf("%d", &post[i]);
                                                      ^
a.cpp:27:52: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 0; i < n; i++) scanf("%d", &in[i]);
                                                    ^
a.cpp:28:20: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &m);
                    ^
a.cpp:58:15: warning: ‘ans’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         printf("%s\n", ans ? "Yes" : "No");
         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~