• PAT(甲级)2019年秋季考试练习


    1160 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<K<10) and m (1<m<90), of which the meanings are given in the problem description.

    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

    分析

    K=6,m=45
    n,A
    A:id1+id2+id3=m=45; 189999
    A+1:i1+i2+i3=n=10; 190000
    grid(m,n)=prime

    题解

    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    bool isPrime(int x) {
        if (x <= 1) return false;
        for (int i = 2, sqr = sqrt(x); i <= sqr; ++i) {
            if (x % i == 0) return false;
        }
        return true;
    }
    
    int gcd(int a, int b) {
        if (b == 0) return a;
        else return gcd(b, a % b);
    }
    
    int digitSum(int x) {
        int sum = 0;
    
        string s = to_string(x);
        for (int i = 0, len = s.length(); i < len; ++i)
            sum += s[i] - '0';
    
        return sum;
    }
    
    struct record {
        int sum, val;
    
        record(int v, int n) : val(v), sum(n) {}
    
        bool operator<(record &x) {
            if (sum != x.sum) return sum < x.sum;
            else return val < x.val;
        }
    };
    
    vector<record> r;
    
    void dfs(int sum, int val, int left, int target) {
        if (left == 0 && sum == target) {
            int n = digitSum(val + 1), g = gcd(sum, n);
            if (g > 2 && isPrime(g)) r.push_back(record(val, n));
        } else if (left > 0)
            for (int i = 0; i <= 9; ++i)
                if (sum + i + left * 9 - 9 >= target && sum + i <= target)
                    dfs(sum + i, val * 10 + i, left - 1, target);
    }
    
    int main() {
    //    freopen("in.txt", "r", stdin);
    //    freopen("out.txt", "w", stdout);
    
        int n, k, m;
        cin >> n;
        for (int i = 1; i <= n; ++i) {
            r.clear();
            cin >> k >> m;
            cout << "Case " << i << "\n";
            for (int j = 1; j <= 9; ++j) dfs(j, j, k - 1, m);
            if (r.empty()) cout << "No Solution\n";
            else {
                sort(r.begin(), r.end());
                for (int j = 0; j < r.size(); ++j)
                    cout << r[j].sum << " " << r[j].val << "\n";
            }
        }
    }
    
    

    1161 Merging Linked Lists (25 分)

    image

    Input Specification:

    Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of L1 and L2, 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

    题解

    太菜了Orz那个vector c添加的部分想了好久,还是没想出来,,,
    原来循环里总体是用k来控制的,当k%3==2的时候就把b数组添加进来,否则添加a。
    脑子就没有这么清晰。。。

    #include <bits/stdc++.h>
    
    using namespace std;
    const int maxn=100000;
    struct Node
    {
        int address,key,next;
    }node[maxn],list1[maxn],list2[maxn];
    int vis[maxn];
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif
        int n,s1,s2,id,k;
        cin>>s1>>s2>>n;
        for(int i=0; i<n; i++)
        {
            cin>>id;
            node[id].address=id;
            cin>>node[id].key>>node[id].next;
        }
        vector<int> a,b,c;
        for(int i=s1;i!=-1;i=node[i].next){
            a.push_back(i);
        }
        for(int i=s2;i!=-1;i=node[i].next){
            b.push_back(i);
        }
        if(a.size()<b.size()) swap(a,b);
        reverse(b.begin(),b.end());
        for(int i=0,j=0,k=0;i<a.size()||j<b.size();k++)
            if(k%3==2&&j<b.size()) c.push_back(b[j++]);
            else c.push_back(a[i++]);
        for(int i=0;i<c.size();i++){
            if(i!=c.size()-1)
                printf("%05d %d %05d\n",c[i],node[c[i]].key,c[i+1]);
            else
                printf("%05d %d -1\n",c[i],node[c[i]].key);
        }
        return 0;
    }
    

    1162 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.
    image
    image

    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<iostream>
    #include<vector>
    using namespace std;
    
    struct node{
        string data;
        int l,r;	//左右孩子的编号
    };
    
    vector<node> v;	//结点数组 
    
    string f(int x){
        if(v[x].l==-1 && v[x].r==-1){	//左右孩子都为空 
            return "("+v[x].data+")";
        }
        if(v[x].l==-1 && v[x].r!=-1){	//左孩子为空右孩子非空 
            return "("+v[x].data+f(v[x].r)+")";
        }
        return "("+f(v[x].l)+f(v[x].r)+v[x].data+")";	//左右都非空 
    }
    
    int main(){
        int n;	//结点数 
        scanf("%d\n",&n);
        v.resize(n+1);
        vector<int> a(n+1);	//用于找到根,非根结点标记为1
        a.clear();
        
        for(int i = 1; i <= n; i++){
            cin >> v[i].data >> v[i].l >> v[i].r;
            if(v[i].l!=-1) a[v[i].l]=1;
            if(v[i].r!=-1) a[v[i].r]=1;
        }
        for(int i = 1; i <= n; i++){
            if(a[i] == 0){
                cout << f(i);
                break;
            }
        }
        
        return 0;
    }
    

    1163 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 Nv(<=10^3) and Ne(<=10^5), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to Nv

    Then Ne 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 Nv 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 <bits/stdc++.h>
    
    using namespace std;
    const int maxn=1010,Max=999999999;
    int e[maxn][maxn],d[maxn];
    vector<int> v;
    int vis[maxn];
    int n,m;
    bool Dijkstra(int s)
    {
        fill(d,d+maxn,Max);
        fill(vis,vis+maxn,false);
        d[s]=0;
        for(int i=0;i<n;i++){
            int u=-1,MIN=Max;
            for(int j=1;j<=n;j++){
                if(vis[j]==false&&d[j]<MIN){
                    u=j;
                    MIN=d[j];
                }
            }
            if(u==-1||d[u]!=d[v[i]]) return false;
            vis[u]=true;
            for(int v=1;v<=n;v++){
                if(vis[v]==false&&e[u][v]!=0){
                    if(d[u]+e[u][v]<d[v])
                        d[v]=d[u]+e[u][v];
                }
            }
        }
        return true;
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif
        int s1,s2,dist,k;
        cin>>n>>m;
        for(int i=0;i<m;i++){
            cin>>s1>>s2>>dist;
            e[s1][s2]=e[s2][s1]=dist;
        }
        cin>>k;
        while(k--){
            v.resize(n);
            for(int i=0;i<n;i++){
                cin>>v[i];
            }
            if(Dijkstra(v[0])) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    Curso de FP Interpretacion Lenguaje de Signos a distancia.
    T1载波与E1载波
    快速以太网中传输介质100BASETX
    MySQLdb
    NRZ编码、NRZI编码、曼彻斯特编码和差分曼彻斯特编码
    静态VLAN和动态VLAN
    Windows用脚本快速修改IP地址(Netsh)
    some skills in Windows
    shell 条件测试
    [转]不要做浮躁的嵌入式工程师
  • 原文地址:https://www.cnblogs.com/moonlight1999/p/15966255.html
Copyright © 2020-2023  润新知