PAT(甲级)2018年春季考试 经验分享和心路历程

系统生成的分析报告:

PAT(甲级)2018年春季考试

开始时间2016/01/01 08:00:00

结束时间2038/01/18 08:00:00

答题时长180分钟

考生agentofPAT

得分89

总分100


编程题得分:89总分:100

A

Look-and-say Sequence

(20分)

Look-and-say sequence is a sequence of integers as the following:

D, D1, D111, D113, D11231, D112213111, ...

where D is in [0, 9] except 1. The (n+1)st number is a kind of description of the nth number. For example, the 2nd number means that there is one D in the 1st number, and hence it is D1; the 2nd number consists of one D (corresponding to D1) and one 1 (corresponding to 11), therefore the 3rd number is D111; or since the 4th number is D113, it consists of one D, two 1's, and one 3, so the next number must be D11231. This definition works for D = 1 as well. Now you are supposed to calculate the Nth number in a look-and-say sequence of a given digit D.

Input Specification:

Each input file contains one test case, which gives D (in [0, 9]) and a positive integer N (≤ 40), separated by a space.

Output Specification:

Print in a line the Nth number in a look-and-say sequence of D.

Sample Input:

1 8

Sample Output:

1123123111

编译器

GXX

代码

// 1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

int main()
{
#ifndef ONLINE_JUDGE
    FILE* S;
    freopen_s(&S, "in.txt", "r", stdin);
#endif // !ONLINE_JUDGE
    string d;int  n;
    cin >> d >> n;
    for (int k = 0; k < n-1; k++) {
        string t;
        for (int i = 0; i < d.size();) {
            int count = 0;
            int j;
            for (j = i; j < d.size(); j++) {
                if (d[i] == d[j]) {
                    count++;
                }
                else {
                    
                    break;
                }
            }
            t += d[i];
            t += count + '0';
            i = j;
        }
        d = t;
        //cout << t<<endl;
    }
    cout << d << endl;
    return 0;
}

评测结果

答案正确(20 分)

编译器输出

a.cpp: In function ‘int main()’:
a.cpp:17:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < d.size();) {
                         ~~^~~~~~~~~~
a.cpp:20:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (j = i; j < d.size(); j++) {
                         ~~^~~~~~~~~~

测试点得分

测试点结果得分耗时内存
0答案正确1213.00 ms444 KB
1答案正确25.00 ms316 KB
2答案正确27.00 ms444 KB
3答案正确111.00 ms328 KB
4答案正确323.00 ms708 KB

B

PAT Ranking of Institutions

(25分)

After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​5​​), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:

ID Score School

where ID is a string of 6 characters with the first one representing the test level: B stands for the basic level, A the advanced level and T the top level; Score is an integer in [0, 100]; and School is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that ID is unique for each testee.

Output Specification:

For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:

Rank School TWS Ns

where Rank is the rank (start from 1) of the institution; School is the institution code (all in lower case); ; TWS is the total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5, where ScoreX is the total score of the testees belong to this institution on level X; and Ns is the total number of testees who belong to this institution.

The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.

Sample Input:

10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu

Sample Output:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

编译器

GXX

代码

// 2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

struct info {
    string id;
    double score;
    int tws;
    int num;
    info() {
        score = 0.0;
        num = 0;
    }
};

vector<info> an;
unordered_map<string, info> record;
//unordered_map < string, double > tws;
//unordered_map<string, int> ns;

string lower(string x) {
    string t; char c;
    for (int i = 0; i < x.size(); i++) {
        t += tolower(x[i]);
    }
    return t;
}

bool cmp(info a, info b) {
    if (a.tws != b.tws) {
        return a.tws > b.tws;
    }
    else if (a.num != b.num) {
        return a.num < b.num;
    }
    else if (a.id != b.id) {
        return a.id < b.id;
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    FILE* S;
    freopen_s(&S, "in.txt", "r", stdin);
#endif // !ONLINE_JUDGE

    int n;
    cin >> n;
    string id, school; int score;
    for (int i = 0; i < n; i++) {
        double a=0;
        cin >> id >> score >> school;
        if (id[0] == 'B') {
            a = score / 1.5;
        }
        else if (id[0] == 'A') {
            a = score ;
        }
        else if (id[0] == 'T') {
            a = score*1.5;
        }
        string b = lower(school);
        record[b].id = b;
        record[b].score += a;
        record[b].num++;
    }
    info* node = new info();
    for (auto it = record.begin(); it != record.end(); it++) {
        node->id = it->second.id;
        node->num = it->second.num;
        node->tws = (int)it->second.score;
        an.push_back(*node);
    }
    cout << an.size()<<endl;
    sort(an.begin(), an.end(), cmp);
    int cur_rank = 1;
    int cur_max = INT_MAX;
    for (int i = 0; i < an.size(); i++) {
        if (an[i].tws < cur_max) {
            printf("%d %s %d %d\n", i + 1, an[i].id.data(), an[i].tws, an[i].num);
            cur_max = an[i].tws;
            cur_rank = i + 1;
        }
        else{//an[i].tws == cur_max
            printf("%d %s %d %d\n", cur_rank, an[i].id.data(), an[i].tws, an[i].num);
        }
    }
    return 0;
}

评测结果

答案正确(25 分)

编译器输出

a.cpp: In function ‘std::__cxx11::string lower(std::__cxx11::string)’:
a.cpp:25:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < x.size(); i++) {
                     ~~^~~~~~~~~~
a.cpp:24:20: warning: unused variable ‘c’ [-Wunused-variable]
     string t; char c;
                    ^
a.cpp: In function ‘int main()’:
a.cpp:81:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < an.size(); i++) {
                     ~~^~~~~~~~~~~
a.cpp: In function ‘bool cmp(info, info)’:
a.cpp:41:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

测试点得分

测试点结果得分耗时内存
0答案正确1512.00 ms464 KB
1答案正确215.00 ms408 KB
2答案正确120.00 ms312 KB
3答案正确28.00 ms332 KB
4答案正确2458.00 ms18552 KB
5答案正确3442.00 ms18868 KB

C

Maximal Clique

(25分)

clique is a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent. A maximal clique is a clique that cannot be extended by including one more adjacent vertex. (Quoted from https://en.wikipedia.org/wiki/Clique_(graph_theory))

Now it is your job to judge if a given subset of vertices can form a maximal clique.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers Nv (≤ 200), the number of vertices in the graph, and Ne, the number of undirected edges. Then Ne lines follow, each gives a pair of vertices of an edge. The vertices are numbered from 1 to Nv.

After the graph, there is another positive integer M (≤ 100). Then M lines of query follow, each first gives a positive number K (≤ Nv), then followed by a sequence of K distinct vertices. All the numbers in a line are separated by a space.

Output Specification:

For each of the M queries, print in a line Yes if the given subset of vertices can form a maximal clique; or if it is a clique but not a maximal clique, print Not Maximal; or if it is not a clique at all, print Not a Clique.

Sample Input:

8 10
5 6
7 8
6 4
3 6
4 5
2 3
8 2
2 7
5 3
3 4
6
4 5 4 3 6
3 2 8 7
2 2 3
1 1
3 4 3 6
3 3 2 1

Sample Output:

Yes
Yes
Yes
Yes
Not Maximal
Not a Clique

编译器

GXX

代码

// 3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

unordered_map<int, vector<int>> gn;

int main()
{
#ifndef ONLINE_JUDGE
    FILE* s;
    freopen_s(&s, "in.txt", "r", stdin);
#endif // !ONLINE_JUDGE

    int nv, ne;
    cin >> nv >> ne;
    int a, b;
    for (int i = 0; i < ne; i++) {
        cin >>a >> b;
        gn[a].push_back(b);
        gn[b].push_back(a);
    }
    int m,k;
    cin >> m;
    for (int i = 0; i < m; i++) {
        cin >> k;
        vector<int> an(k);
        for (int j = 0; j < k; j++) {
            cin >> an[j];
        }
        bool clique = true;
        for (int j = 0; j < k; j++) {
            if (!clique)break;
            vector<bool> adj(nv + 1, false);
            for (int p = 0; p < gn[an[j]].size(); p++) {
                adj[gn[an[j]][p]]=true;
            }
            for (int p = j + 1; p < k; p++) {
                if (adj[an[p]] == false) {
                    clique = false;
                    break;
                }
            }
        }
        if (clique) {
            bool Maximal = true;
            vector<bool> add(nv + 1, false);
            for (int j = 0; j < k; j++) {
                add[an[j]] = true;
            }
            for (int q = 1; q <=nv; q++) {
                if (add[q] == false) {//还未加入的点
                    if (!Maximal)break;
                    vector<bool> adj(nv + 1, false);
                    for (int p = 0; p < gn[q].size(); p++) {
                        adj[gn[q][p]] = true;
                    }
                    int count = 0;
                    for (int r = 0; r < an.size(); r++) {
                        if (adj[an[r]] == true) {
                            count++;
                        }
                    }
                    if (count == an.size()) {
                        Maximal = false;
                    }
                }
            }
            if (Maximal) {
                cout<< "Yes\n";
            }
            else {
                cout << "Not Maximal\n";
            }
        }
        else {
            cout << "Not a Clique\n";
        }
    }
    return 0;
}

评测结果

答案正确(25 分)

编译器输出

a.cpp: In function ‘int main()’:
a.cpp:36:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int p = 0; p < gn[an[j]].size(); p++) {
                             ~~^~~~~~~~~~~~~~~~~~
a.cpp:56:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                     for (int p = 0; p < gn[q].size(); p++) {
                                     ~~^~~~~~~~~~~~~~
a.cpp:60:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                     for (int r = 0; r < an.size(); r++) {
                                     ~~^~~~~~~~~~~
a.cpp:65:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                     if (count == an.size()) {
                         ~~~~~~^~~~~~~~~~~~

测试点得分

测试点结果得分耗时内存
0答案正确134.00 ms444 KB
1答案正确44.00 ms316 KB
2答案正确23.00 ms324 KB
3答案正确387.00 ms572 KB
4答案正确35.00 ms448 KB

D

Lowest Common Ancestor

(30分)

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.

A binary search tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

Given any two nodes in a BST, you are supposed to find their LCA.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.

Output Specification:

For each given pair of U and V, print in a line LCA of U and V is A. if the LCA is found and A is the key. But if A is one of U and V, print X is an ancestor of Y. where X is A and Y is the other node. If U or V is not found in the BST, print in a line ERROR: U is not found. or ERROR: V is not found. or ERROR: U and V are not found..

Sample Input:

6 8
6 3 1 2 5 4 8 7
2 5
8 7
1 9
12 -3
0 8
99 99

Sample Output:

LCA of 2 and 5 is 3.
8 is an ancestor of 7.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.

编译器

GXX

代码

// 4.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

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

int m, n, t;
struct node
{
    int val;
    node* left;
    node* right;
    node() {
        left = right = NULL;
    }
};
vector<int> pre;
vector<int> in;
unordered_map<int, int> parent;
unordered_map<int, bool> visit;


void DFS(node* root) {
    if (root->left != NULL) {
        parent[root->left->val] = root->val;
        DFS(root->left);
    } 
    if (root->right != NULL) {
        parent[root->right->val] = root->val;
        DFS(root->right);
    }
        
}

node* build(int start,int begin,int end) {
    if (begin > end || start >= n) {
        return NULL;
    }
    int i;
    for (i = 0; i < n; i++) {
        if (in[i] == pre[start]) {
            break;
        }
    }
    node* temp = new node();
    temp->val = in[i];
    temp->left = build(start + 1, start, i - 1);
    temp->right = build(start + 1 + i, i + 1, end);
    return temp;
}

void mark(int x,int root) {
    while (x!=root)
    {
        visit[x] = true;
        x = parent[x];
    }
    visit[root] = true;
}

int LCA(int u, int v,int root) {
    mark(u, root);
    while (visit[v] != true) {
        v = parent[v];
    }
    return v;
}

int main()
{
#ifndef ONLINE_JUDGE
    FILE* s;
    freopen_s(&s, "in.txt", "r", stdin);
#endif // !ONLINE_JUDGE

    cin >> m >> n;
    for (int i = 0; i < n; i++) {
        cin >> t;
        pre.push_back(t);
        in.push_back(t);
    }
    sort(in.begin(), in.end());
    node* root = build(0, 0, n - 1);//input
    DFS(root);//parent
    int x, y;
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        auto itx = parent.find(x);
        auto ity = parent.find(y);
        if (itx == parent.end() || ity == parent.end()) {
            if (itx != parent.end() && ity == parent.end()) {
                printf("ERROR: %d is not found.\n", y);
            }
            else if (itx == parent.end() && ity != parent.end()) {
                printf("ERROR: %d is not found.\n", x);
            }
            else {
                printf("ERROR: %d and %d are not found.\n", x,y);
            }
        }
        else {
            int z = LCA(x, y, root->val);
            if (z != x && z != y) {
                printf("LCA of %d and %d is %d.\n", x,y,z);
            }
            else if (z == x && z != y) {
                printf("%d is an ancestor of %d.\n", x, y);
            }
            else if (z != x && z == y) {
                printf("%d is an ancestor of %d.\n", y, x);
            }
        }
    }
    return 0;
}


评测结果

部分正确(19 分)

测试点得分

测试点结果得分耗时内存
0答案正确155.00 ms456 KB
1答案错误04.00 ms340 KB
2答案错误04.00 ms324 KB
3答案正确48.00 ms584 KB
4答案错误034.00 ms1216 KB
5答案错误015.00 ms712 KB

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页