• 2020春季PAT甲级满分题解


    7-1 Prime Day (20分)

    The above picture is from Sina Weibo, showing May 23rd, 2019 as a very cool "Prime Day". That is, not only that the corresponding number of the date 20190523 is a prime, but all its sub-strings ended at the last digit 3 are prime numbers.

    Now your job is to tell if a given date is a Prime Day.

    Input Specification:

    Each input file contains one test case. For each case, a date between January 1st, 0001 and December 31st, 9999 is given, in the format yyyymmdd.

    Output Specification:

    For each given date, output in the decreasing order of the length of the substrings, each occupies a line. In each line, print the string first, followed by a space, then Yes if it is a prime number, or No if not. If this date is a Prime Day, print in the last line All Prime!.

    Sample Input 1:

    20190523
    
     

    Sample Output 1:

    20190523 Yes
    0190523 Yes
    190523 Yes
    90523 Yes
    0523 Yes
    523 Yes
    23 Yes
    3 Yes
    All Prime!
    
     

    Sample Input 2:

    20191231
    
     

    Sample Output 2:

    20191231 Yes
    0191231 Yes
    191231 Yes
    91231 No
    1231 Yes
    231 No
    31 Yes
    1 No
    
     
     1 #include <iostream>
     2 #include <cstdio> 
     3 #include <cmath>
     4 #include <string>
     5 
     6 using namespace std;
     7 
     8 bool judge(int n)
     9 {
    10     if(n == 1 || n == 0)
    11         return false;
    12         
    13     int x = sqrt(n);
    14     for(int i = 2; i <= x; ++i)
    15     {
    16         if(n%i == 0)
    17             return false;
    18     }
    19     
    20     return true;
    21 }
    22 
    23 int main()
    24 {
    25     string str;
    26     cin >> str;
    27     int flag = 0;
    28     for(int i = 0; i < str.length(); ++i)
    29     {
    30         string s = str.substr(i);
    31         int num = stoi(s);
    32         
    33         if(judge(num))
    34         {
    35             cout << s << ' ' << "Yes" << endl;
    36         }
    37         else
    38         {
    39             cout << s << ' ' << "No" << endl;
    40             flag = 1;
    41         }
    42     }
    43     
    44     if(flag == 0)
    45         cout << "All Prime!";
    46     
    47     
    48     return 0;
    49 }

    7-2 The Judger (25分)

    A game of numbers has the following rules: at the beginning, two distinct positive integers are given by the judge. Then each player in turn must give a number to the judge. The number must be the difference of two numbers that are previously given, and must not be duplicated to any of the existed numbers. The game will run for several rounds. The one who gives a duplicate number or even a wrong number will be kicked out.

    Your job is to write a judger program to judge the players' numbers and to determine the final winners.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives two distinct positive integers to begin with. Both numbers are in [.

    In the second line, two numbers are given: N (2), the number of players, and M (2), the number of rounds.

    Then N lines follow, each contains M positive integers. The i-th line corresponds to the i-th player (,). The game is to start from the 1st player giving his/her 1st number, followed by everybody else giving their 1st numbers in the 1st round; then everyone give their 2nd numbers in the 2nd round, and so on so forth.

    Output Specification:

    If the i-th player is kicked out in the k-th round, print in a line Round #k: i is out.. The rest of the numbers given by the one who is out of the game will be ignored. If more than one player is out in the same round, print them in increasing order of their indices. When the game is over, print in the last line Winner(s): W1 W2 ... Wn, where W1 ... Wn are the indices of the winners in increasing order. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line. If there is no winner, print No winner. instead.

    Sample Input 1:

    101 42
    4 5
    59 34 67 9 7
    17 9 8 50 7
    25 92 43 26 37
    76 51 1 41 40
    
     

    Sample Output 1:

    Round #4: 1 is out.
    Round #5: 3 is out.
    Winner(s): 2 4
    
     

    Sample Input 2:

    42 101
    4 5
    59 34 67 9 7
    17 9 18 50 49
    25 92 58 1 39
    102 32 2 6 41
    
     

    Sample Output 2:

    Round #1: 4 is out.
    Round #3: 2 is out.
    Round #4: 1 is out.
    Round #5: 3 is out.
    No winner.

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <set>
     5 #include <unordered_set>
     6 
     7 using namespace std;
     8 
     9 int a, b;
    10 int m, n;
    11 int g[12][1100];
    12 int vis[1100000];    //已经有的数字 
    13 int cor[1100000];    //已经有的差值 
    14 int kicked[12];        //该选手是否被淘汰 
    15 unordered_set<int> se;
    16 
    17 int main()
    18 {
    19     scanf("%d%d", &a, &b);
    20     scanf("%d%d", &n, &m);
    21     se.insert(a);
    22     se.insert(b);
    23     
    24     cor[abs(a-b)] = 1;
    25     
    26     for(int i = 1; i <= n; ++i)
    27     {
    28         for(int j = 1; j <= m; ++j)
    29         {
    30             scanf("%d", &g[i][j]);
    31         }
    32     }
    33     
    34     
    35     int tag = 0;
    36     int kickedNum = 0;
    37     for(int j = 1; j <= m; ++j)
    38     {
    39         for(int i = 1; i <= n; ++i)
    40         {
    41             if(cor[g[i][j]] == 1 && se.count(g[i][j]) == 0 && kicked[i] == 0)
    42             {
    43                 // 数字有效 
    44                 for(auto e: se)
    45                 {
    46                     cor[abs(e-g[i][j])]    = 1;
    47                 } 
    48                 se.insert(g[i][j]);
    49             }
    50             else
    51             {
    52                 if(kicked[i] == 0)
    53                 {
    54                     printf("Round #%d: %d is out.
    ", j, i);
    55                     kickedNum++;
    56                     kicked[i] = 1;
    57                     
    58 //                    for(auto e: se)
    59 //                    {
    60 //                        cor[abs(e-g[i][j])]    = 1;
    61 //                    } 
    62 //                    se.insert(g[i][j]);
    63                     if(kickedNum == n)
    64                     {
    65                         tag = 1;
    66                         break;
    67                     }
    68                 }
    69             }
    70                 
    71         }
    72         
    73         if(tag == 1)
    74             break;    
    75     }
    76     
    77     if(tag == 0)
    78     {
    79         printf("Winner(s):");
    80         for(int i = 1; i <= n; ++i)
    81         {
    82             if(kicked[i] == 0)
    83             {
    84                 printf(" %d", i);
    85             }    
    86         }
    87     }
    88     else
    89     {
    90         printf("No winner.");
    91     }
    92     
    93     
    94     return 0;
    95 }

    7-3 Safari Park (25分)

    A safari park(野生动物园)has K species of animals, and is divided into N regions. The managers hope to spread the animals to all the regions, but not the same animals in the two neighboring regions. Of course, they also realize that this is an NP complete problem, you are not expected to solve it. Instead, they have designed several distribution plans. Your job is to write a program to help them tell if a plan is feasible.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives 3 integers: N (0), the number of regions; R (≥), the number of neighboring relations, and K (0), the number of species of animals. The regions and the species are both indexed from 1 to N.

    Then R lines follow, each gives the indices of a pair of neighboring regions, separated by a space.

    Finally there is a positive M (≤) followed by M lines of distribution plans. Each plan gives N indices of species in a line (the i-th index is the animal in the i-th rigion), separated by spaces. It is guaranteed that any pair of neighboring regions must be different, and there is no duplicated neighboring relations.

    Output Specification:

    For each plan, print in a line Yes if no animals in the two neighboring regions are the same, or No otherwise. However, if the number of species given in a plan is not K, you must print Error: Too many species.or Error: Too few species. according to the case.

    Sample Input:

    6 8 3
    2 1
    1 3
    4 6
    2 5
    2 4
    5 4
    5 6
    3 6
    5
    1 2 3 3 1 2
    1 2 3 4 5 6
    4 5 6 6 4 5
    2 3 4 2 3 4
    2 2 2 2 2 2
    
     

    Sample Output:

    Yes
    Error: Too many species.
    Yes
    No
    Error: Too few species.


     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string>
     4 #include <vector>
     5 #include <set>
     6 #include <cstring>
     7 
     8 using namespace std;
     9 
    10 
    11 int n, r, k;
    12 int g[510][510];
    13 int m;
    14 int f[510];
    15 set<int> se;
    16 vector<int> adjList[510];
    17 
    18 int main()
    19 {
    20     scanf("%d%d%d", &n,&r,&k);
    21     for(int i = 0; i < r; ++i)
    22     {
    23         int a, b;
    24         scanf("%d%d", &a, &b);
    25         adjList[a].push_back(b);
    26         adjList[b].push_back(a);
    27     }
    28     scanf("%d", &m);
    29     for(int i = 1; i <= m; ++i)
    30     {
    31         se.clear();
    32         memset(f, 0, sizeof(f));
    33         for(int j = 1; j <= n; ++j)
    34         {
    35             scanf("%d", &f[j]);
    36             se.insert(f[j]);
    37         }
    38         
    39         if(se.size() > k)
    40         {
    41             printf("Error: Too many species.
    ");
    42         }
    43         else if(se.size() < k)
    44         {
    45             printf("Error: Too few species.
    ");
    46         }
    47         else
    48         {
    49             int flag = 0;
    50             for(int k = 1; k <= n; ++k)
    51             {
    52                 for(int t = 0; t < adjList[k].size(); ++t)
    53                 {
    54                     if(f[k] == f[adjList[k][t]])
    55                     {
    56                         printf("No
    ");
    57                         flag = 1;
    58                         break;
    59                     }
    60                 }
    61                 if(flag == 1)
    62                     break;
    63             }
    64             
    65             if(flag == 0)
    66             {
    67                 printf("Yes
    ");
    68             }
    69         }
    70         
    71     }
    72     
    73     
    74     return 0;
    75 }

    7-4 Replacement Selection (30分)

    When the input is much too large to fit into memory, we have to do external sorting instead of internal sorting. One of the key steps in external sorting is to generate sets of sorted records (also called runs) with limited internal memory. The simplest method is to read as many records as possible into the memory, and sort them internally, then write the resulting run back to some tape. The size of each run is the same as the capacity of the internal memory.

    Replacement Selection sorting algorithm was described in 1965 by Donald Knuth. Notice that as soon as the first record is written to an output tape, the memory it used becomes available for another record. Assume that we are sorting in ascending order, if the next record is not smaller than the record we have just output, then it can be included in the run.

    For example, suppose that we have a set of input { 81, 94, 11, 96, 12, 99, 35 }, and our memory can sort 3 records only. By the simplest method we will obtain three runs: { 11, 81, 94 }, { 12, 96, 99 } and { 35 }. According to the replacement selection algorithm, we would read and sort the first 3 records { 81, 94, 11 } and output 11 as the smallest one. Then one space is available so 96 is read in and will join the first run since it is larger than 11. Now we have { 81, 94, 96 }. After 81 is out, 12 comes in but it must belong to the next run since it is smaller than 81. Hence we have { 94, 96, 12 } where 12 will stay since it belongs to the next run. When 94 is out and 99 is in, since 99 is larger than 94, it must belong to the first run. Eventually we will obtain two runs: the first one contains { 11, 81, 94, 96, 99 } and the second one contains { 12, 35 }.

    Your job is to implement this replacement selection algorithm.

    Input Specification:

    Each input file contains several test cases. The first line gives two positive integers N (≤) and M (<), which are the total number of records to be sorted, and the capacity of the internal memory. Then Nnumbers are given in the next line, all in the range of int. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print in each line a run (in ascending order) generated by the replacement selection algorithm. All 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.

    Sample Input:

    13 3
    81 94 11 96 12 99 17 35 28 58 41 75 15
    
     

    Sample Output:

    11 81 94 96 99
    12 17 28 35 41 58 75
    15
    
     
      1 #include <iostream>
      2 #include <string>
      3 #include <cstdio>
      4 #include <vector>
      5 #include <deque>
      6 #include <algorithm>
      7 #include <queue>
      8 
      9 using namespace std;
     10 
     11 int n, m;
     12 int a[100010];
     13 priority_queue<int, vector<int> ,greater<int> > input[10010];
     14 vector<int> output[10010];
     15 
     16 void bubble(deque<int>& d)
     17 {
     18     int dsize = d.size();
     19     for(int p = dsize-1; p >= 1; --p)
     20     {
     21         int flag = 0;
     22         for(int i = 0; i < p; ++i)
     23         {
     24             if(d[i] > d[i+1])
     25             {
     26                 swap(d[i], d[i+1]);
     27                 flag = 1;
     28             }
     29         }
     30         
     31         if(flag == 0)
     32             break;
     33     }
     34     
     35     
     36 }
     37 
     38 int main()
     39 {
     40     scanf("%d%d", &n, &m);
     41     for(int i = 0; i < n; ++i)
     42         scanf("%d", &a[i]);
     43     
     44     int icnt = 0;
     45     int ocnt = 0;
     46     for(int i = 0; i < m; ++i)
     47     {
     48         input[icnt].push(a[i]);
     49     }
     50     
     51     
     52     for(int i = m; i < n; ++i)
     53     {
     54         int tmp = input[icnt].top();
     55         output[ocnt].push_back(tmp);
     56         input[icnt].pop();
     57         
     58         if(a[i] < tmp)
     59         {
     60             input[icnt+1].push(a[i]);
     61         }
     62         else
     63         {
     64             input[icnt].push(a[i]);
     65         }
     66         
     67         if(input[icnt].empty())
     68         {
     69             icnt++;
     70             ocnt++;
     71         }
     72     }
     73     
     74     
     75     while(!input[icnt].empty())
     76     {
     77         int tmp = input[icnt].top();
     78         output[ocnt].push_back(tmp);
     79         input[icnt].pop();
     80         
     81         if(input[icnt].empty())
     82         {
     83             icnt++;
     84             ocnt++;
     85         }    
     86     }    
     87     
     88     for(int j = 0; j < ocnt; ++j)
     89     {
     90         for(int x = 0; x < output[j].size(); ++x)
     91         {
     92             if(x == output[j].size()-1)
     93                 printf("%d", output[j][x]);
     94             else    
     95                 printf("%d ", output[j][x]);
     96         }
     97         if(j != ocnt-1)
     98             printf("
    ");
     99     }
    100     
    101     return 0;
    102 }
     
  • 相关阅读:
    php_l3arning_notes_3
    php_l3arning_notes_2
    php_l3arning_notes_1
    从 HTTP 到 HTTPS 再到 HSTS
    WSGI&ASGI
    WSGI 介绍
    什么是multipart/form-data请求
    SSH 端口转发
    Redis与RabbitMQ作为消息队列的比较
    数据库索引,到底是什么做的?
  • 原文地址:https://www.cnblogs.com/FengZeng666/p/13620974.html
Copyright © 2020-2023  润新知