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

7-1 Forever (20分)
"Forever number" is a positive integer A with K digits, satisfying the following constrains:

  • the sum of all the digits of A is m;
  • the sum of all the digits of A+1 is n; and
  • the greatest common divisor of m and n is a prime number which is greater than 2.

Now you are supposed to find these forever numbers.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer N (≤5). Then N lines follow, each gives a pair of K (3

Output Specification:

For each pair of K and m, first print in a line Case X, where X is the case index (starts from 1). Then print n and A in the following line. The numbers must be separated by a space. If the solution is not unique, output in the ascending order of n. If still not unique, output in the ascending order of A. If there is no solution, output No Solution.

Sample Input:

2
6 45
7 80

Sample Output:

Case 1
10 189999
10 279999
10 369999
10 459999
10 549999
10 639999
10 729999
10 819999
10 909999
Case 2
No Solution

参考代码:

#include 
using namespace std;

struct node{//注意数位和是num+1后的数位和
    int d_sum, num;
};

int n, k, m;
vector temp;
vector ans;

int digit_sum(int a){
    int sum = 0;
    while(a){
        sum += a%10;
        a /= 10;
    }
    return sum;
}

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

int gcd(int a, int b){
    return !b ? a : gcd(b, a%b);
}

bool cmp(node a, node b){
    return a.d_sum == b.d_sum ? a.num < b.num : a.d_sum < b.d_sum;
}

void DFS(int sum, int dep){
    if(sum > m || sum+(k-dep)*9 < m)     return;
    if(dep == k){
        if(sum == m){
            int num = 0, t_sum;  //求取目前所有数位组成的数字以及加一后的数位和
            for(int i = 0; i < temp.size(); i++)    num = num*10 + temp[i];
            t_sum = digit_sum(num+1);
            if(is_prime(gcd(sum, t_sum)))   ans.push_back(node{t_sum, num});
        }
        return;
    }
    for(int i = 0; i <= 9; i++){
        temp.push_back(i);
        DFS(sum+i, dep+1);
        temp.pop_back();
    }
}

int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        printf("Case %d\n", i);
        scanf("%d %d", &k, &m); //k为数位,m为数字的各位和
        ans.clear();
        for(int i = 1; i <= 9; i++){
            temp.push_back(i);
            DFS(i, 1);
            temp.pop_back();
        }
        if(ans.empty())    printf("No Solution\n");
        else{
            sort(ans.begin(), ans.end(), cmp);
            for(int j = 0; j < ans.size(); j++)     printf("%d %d\n", ans[j].d_sum, ans[j].num);
        }
    }
    return 0;
}

7-2 Merging Linked Lists (25分)
Given two singly linked lists L​1​​=a​1​​→a​2​​→⋯→a​n−1​​→a​n​​ and L​2​​=b​1​​→b​2​​→⋯→b​m−1​​→b​m​​. If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a​1​​→a​2​​→b​m​​→a​3​​→a​4​​→b​m−1​​⋯. For example, given one list being 6→7 and the other one 1→2→3→4→5, you must output 1→2→7→3→4→6→5.

Input Specification:

Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of L​1​​ and L​2​​, plus a positive N (≤10^​5​​) which is the total number of nodes given. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is a positive integer no more than 10^​5​​, and Next is the position of the next node. It is guaranteed that no list is empty, and the longer list is at least twice as long as the shorter one.

Output Specification:

For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1

Sample Output:

01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1

参考代码:

#include 
using namespace std;

struct node{
    int Address, Data, Next;
}link[100010];

int main(){
    int head1, head2, n;
    int add, dat, nex;
    vector ans1;
    vector ans2;
    vector ans;
    scanf("%d %d %d", &head1, &head2, &n);
    for(int i = 0; i < n; i++){
        scanf("%d %d %d", &add, &dat, &nex);
        link[add].Address = add;
        link[add].Data = dat;
        link[add].Next = nex;
    }
    while(head1 != -1){
        ans1.push_back(link[head1]);
        head1 = link[head1].Next;
    }
    while(head2 != -1){
        ans2.push_back(link[head2]);
        head2 = link[head2].Next;
    }
    int pos = 0;
    if(ans1.size() > ans2.size()){
        reverse(ans2.begin(), ans2.end());
        for(int i = 0; i < ans1.size(); i++){
            ans.push_back(ans1[i]);
            if((i+1)%2 == 0 && pos < ans2.size())   ans.push_back(ans2[pos++]);
        }
    }else{
        reverse(ans1.begin(), ans1.end());
        for(int i = 0; i < ans2.size(); i++){
            ans.push_back(ans2[i]);
            if((i+1)%2 == 0 && pos < ans1.size())   ans.push_back(ans1[pos++]);
        }
    }
    for(int i = 0; i < ans.size(); i++){
        printf("%05d %d", ans[i].Address, ans[i].Data);
        if(i == ans.size()-1)   printf(" -1\n");
        else    printf(" %05d\n", ans[i+1].Address);
    }
    return 0;
}

7-3 Postfix Expression (25分)
Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:

data left_child right_child

where data is a string of no more than 10 characters, left_child and right_child are the indices of this node's left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by −1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.

infix1.JPG

PAT(甲级)2019年秋季考试题解_第1张图片

Figure 1

Figure 2

Output Specification:

For each case, print in a line the postfix expression, with parentheses reflecting the precedences of the operators.There must be no space between any symbols.

Sample Input 1:

8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1

Sample Output 1:

(((a)(b)+)((c)(-(d))*)*)

Sample Input 2:

8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1

Sample Output 2:

(((a)(2.35)*)(-((str)(871)%))+)

参考代码:

#include 
using namespace std;

struct node{
    string data;
    int l, r;
}tree[25];

int is_root[25] = {0};  //只能初始化为0或-1而不能是1

void DFS(int s){
    if(s == -1) return;
    int flag = 1;
    cout << "(";
    if(tree[s].l == -1 && tree[s].r != -1 && (tree[s].data == "-" || tree[s].data == "+")){
        cout << tree[s].data;
        flag = 0;
    }
    DFS(tree[s].l);
    DFS(tree[s].r);
    if(flag)    cout << tree[s].data;
    cout << ")";
}

int main(){
    int n, root;
    string s1, s2, s3;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        cin >> tree[i].data >> tree[i].l >> tree[i].r;
        is_root[tree[i].l] = 1;
        is_root[tree[i].r] = 1;
    }
    for(int i = 1; i <= n; i++){
        if(!is_root[i]){
            root = i;
            break;
        }
    }
    DFS(root);
    cout << endl;
    return 0;
}

7-4 Dijkstra Sequence (30分)
Dijkstra's algorithm is one of the very famous greedy algorithms. It is used for solving the single source shortest path problem which gives the shortest paths from one particular source vertex to all the other vertices of the given graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.

In this algorithm, a set contains vertices included in shortest path tree is maintained. During each step, we find one vertex which is not yet included and has a minimum distance from the source, and collect it into the set. Hence step by step an ordered sequence of vertices, let's call it Dijkstra sequence, is generated by Dijkstra's algorithm.

On the other hand, for a given graph, there could be more than one Dijkstra sequence. For example, both { 5, 1, 3, 4, 2 } and { 5, 3, 1, 2, 4 } are Dijkstra sequences for the graph, where 5 is the source. Your job is to check whether a given sequence is Dijkstra sequence or not.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive integers N​v​​ (≤10​^3​​) and N​e​​ (≤10​^5​​), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to N​v​​.

Then N​e​​ lines follow, each describes an edge by giving the indices of the vertices at the two ends, followed by a positive integer weight (≤100) of the edge. It is guaranteed that the given graph is connected.

Finally the number of queries, K, is given as a positive integer no larger than 100, followed by K lines of sequences, each contains a permutationof the N​v​​ vertices. It is assumed that the first vertex is the source for each sequence.

All the inputs in a line are separated by a space.

Output Specification:

For each of the K sequences, print in a line Yes if it is a Dijkstra sequence, or No if not.

Sample Input:

5 7
1 2 2
1 5 1
2 3 1
2 4 1
2 5 2
3 5 1
3 4 1
4
5 1 3 4 2
5 3 1 2 4
2 3 4 5 1
3 2 1 5 4

Sample Output:

Yes
Yes
Yes
No

参考代码:

#include 
using namespace std;

const int maxv = 1010;
const int INF = 0x3fffffff;

int Nv, Ne;
int d[maxv];
int G[maxv][maxv];
bool vis[maxv];
queue q;

bool dijkstra(){
    fill(d, d+maxv, INF);
    fill(vis, vis+maxv, false);
    int s = q.front();
    d[s] = 0;
    for(int i = 0; i < Nv; i++){
        int u = q.front();
        q.pop();
        int Min = d[u];
        if(Min == INF)  return false;
        for(int j = 1; j <= Nv; j++){
            if(!vis[j] && d[j] < Min)  return false;
        }
        vis[u] = true;
        for(int v = 1; v <= Nv; v++){
            if(!vis[v] && G[u][v] != INF){
                if(d[u] + G[u][v] < d[v])   d[v] = d[u] + G[u][v];
            }
        }
    }
    return true;
}

int main(){
    int v1, v2, temp;
    scanf("%d %d", &Nv, &Ne);
    fill(G[0], G[0]+maxv*maxv, INF);
    for(int i = 0; i < Ne; i++){
        scanf("%d %d %d", &v1, &v2, &temp);
        G[v1][v2] = G[v2][v1] = temp;
    }
    int k, check;
    scanf("%d", &k);
    for(int i = 0; i < k; i++){
        while(!q.empty())   q.pop();
        for(int j = 1; j <= Nv; j++){
            scanf("%d", &check);
            q.push(check);
        }
        if(dijkstra())  printf("Yes\n");
        else    printf("No\n");
    }
    return 0;
}

你可能感兴趣的:(算法程序员)