当前位置:网站首页>Winter 2021 pat class B problem solution (C language)

Winter 2021 pat class B problem solution (C language)

2022-07-06 05:42:00 Python's path to immortality

Preface : While in good condition , I did this set of real problems again . This time it was done smoothly AC It fell off 1,2,4,5 Four questions , But what's extraordinary is , I finally corrected the third question in the last exam , This time, I can't change it in any way .

notes : This set of real problems C++ For the full score code of the version, please refer to this article after I finished the exam :2021 Year winter PAT Grade B Full Score repeat ( It's not my code )

7-1 Automatic packing machine (15 branch )
 Insert picture description here
The working procedure of a Hami melon automatic packaging pipeline is as follows : First, the system sets the total weight of each box of Hami melon W; Then the conveyor belt conveys Hami melons to an automatic weighing device , Carry out the following operations according to the weighing results :

If the total weight on the scale just reaches W, Then pack all the cantaloupes on the scale and send them away ;
If the total weight on the scale is less than W, Leave this Hami melon on the scale ;
If the total weight on the scale exceeds W, Then put this Hami melon aside for the time being .

Please write a program to count , How many Hami melons are packed in how many boxes ?

Input format :
The first line of input gives two positive integers N(≤1000) and W(≤10^4), They are the number of cantaloupes on the conveyor belt and the specified weight of each box ( g ). The next line shows N A positive integer , Is the weight of each cantaloupe on the conveyor belt , The weight of a single fruit does not exceed 2000 g . It is assumed that the conveyor belt transmits the Hami melon to the weighing equipment in the order of input .

Output format :
Output the number of successfully boxed cases and the number of boxed Hami melons in one line . Between the numbers 1 Space separation , There must be no extra space at the beginning and end of the line .

sample input :
12 5000
2000 1500 1800 1000 1800 500 1900 1500 2000 1600 2000 2000

sample output :
2 7

Sample explanation :
The first 1、2、4、6 Just a box of melons ; The first 7、8、10 Just a box of melons .

AC Code :

#include <stdio.h>
int main(){ 
    
    int n, w, i, x, sum = 0, cnt_xz = 0, cnt_hmg = 0, m = 0;
    scanf("%d %d", &n, &w);
    for(i = 0; i < n; i++){ 
    
        scanf("%d", &x);
        int temp = sum + x;
        if(temp < w){ 
    
            sum += x;
            cnt_hmg++;
            m++;
        }else if(temp == w){ 
    
            cnt_xz++;
            cnt_hmg++;
            sum = 0;
            m = 0;
        }
    }
    if(m) cnt_hmg -= m;
    printf("%d %d", cnt_xz, cnt_hmg);
    return 0;
}

7-2 Medication statistics (20 branch )
 Insert picture description here
The picture above is a question of knowledge :“ In the computer , Is there a way to import a lot of medical records into , Then use the computer to count which languages are more medicinal .?” Although there are typos and punctuation errors in this problem , But kind-hearted, you'd better make one for him —— Given N Medication records in the patient's medical record , Please count the most used drugs , And list the diseases they have treated .

Input format :
Enter the first line to give a positive integer N(≤10^4), Is the number of medical records . And then N That's ok , Each line gives a piece of medical record information , The format is as follows :
Disease number K Drug number 1 Drug number 2 …… Drug number K
The disease number is 4 Digit number , The title ensures that the disease number in each medical record is different ;K No more than 10 The positive integer , Number of types of drugs used for the disease ; The drug number is a number with MD start 、 Followed by 5 A string of digits . Make sure there are no duplicate drug numbers in a medical record .

Output format :
First, output the number of the most used drug in the first line , And the number of times it was used , Separated by a space . If there is a tie , Only the smallest number is output ( namely MD The last string of numbers is the smallest ) the . Then, the number of the disease treated by the drug is output in the order of input , Each row of a .

sample input :
5
0012 3 MD10031 MD99132 MD42107
1024 2 MD99132 MD34821
2048 2 MD27845 MD10031
0149 2 MD34821 MD55802
0035 3 MD55802 MD99132 MD10031

sample output :
MD10031 3
0012
2048
0035

AC Code :

#include <stdio.h>
struct bingli{ 
    
    int bing, k, yao[10];
}bl[10000];
int main(){ 
    
    int n, i, j, cnt[100000] = { 
    0}, max = 0, maxbh;
    scanf("%d", &n);
    for(i = 0;i < n;i++){ 
    
        scanf("%d %d ", &bl[i].bing, &bl[i].k);
        for(j = 0; j < bl[i].k;j++){ 
    
            getchar();
            getchar();
            scanf("%d ", &bl[i].yao[j]);
            cnt[bl[i].yao[j]]++;
            max = max > cnt[bl[i].yao[j]] ? max : cnt[bl[i].yao[j]];
        }
    }
    for(i = 0;i < 100000;i++){ 
    
        if(cnt[i] == max){ 
    
            maxbh = i;
            break;
        }
    }
    printf("MD%05d %d\n", maxbh, max);
    for(i = 0; i < n;i++){ 
    
        for(j = 0; j < bl[i].k;j++){ 
    
            if(bl[i].yao[j] == maxbh){ 
    
                printf("%04d\n", bl[i].bing);
                break;
            }
        }
    }
    return 0;
}

7-3 Colorful black (20 branch )
 Insert picture description here
 Insert picture description here
AC Code :
It was also used in the offline exam last time C Written language , Finally, the problem was solved without timeout . This time, it feels like the code I wrote last time , However, the last two points can never be changed . Here is the post for my use this time C Written language 14 Sub code , Please help me to see how to change this timeout .
 Insert picture description here

#include <stdio.h>
#include <string.h>
int main(){ 
    
    char color[100000][12], res[100000][12];
    int n, i, j, cnt = 0;
    scanf("%d", &n);
    for(i = 0;i < n;i++){ 
    
        scanf("%s", color[i]);
        int flag = 1;
        for(j = 0;j < i;j++){ 
    
            if(!strcmp(color[i], color[j])){ 
    
                flag = 0;
                break;
            }
        }
        if(flag) strcpy(res[cnt++], color[i]);
    }
    printf("%d\n", cnt);
    for(i = 0;i < cnt;i++){ 
    
        if(i) printf(" ");
        printf("%s", res[i]);
    }
    return 0;
}

7-4 Fake news (20 branch )
 Insert picture description here
 Insert picture description here
AC Code :

#include <stdio.h>
int main(){ 
    
    int n, m, i, o[10000], cnt_jwz[10000], max = 0, bh;
    scanf("%d %d", &n, &m);
    while(m--){ 
    
        int cnt_gd[20001] = { 
    0};
        for(i = 0;i < n; i++){ 
    
            scanf("%d", &o[i]);
            if(o[i] < 0) o[i] = o[i] * -1 + 10000;
            cnt_gd[o[i]] ++;
        }
        int min = 100000;
        for(i = 0;i < n; i++) min = min < cnt_gd[o[i]] ? min : cnt_gd[o[i]];
        for(i = 0;i < n; i++){ 
    
            if(min == cnt_gd[o[i]]) cnt_jwz[i]++;
        }
    }
    for(i = 0;i < n; i++){ 
    
        if(cnt_jwz[i] > max){ 
    
            max = cnt_jwz[i];
            bh = i;
        }
    }
    printf("%d", bh + 1);
    return 0;
}

7-5 Rank of static linked list (25 branch )
 Insert picture description here
AC Code :

#include <stdio.h>
int main(){ 
    
    int n, i, t, m = -1;
    scanf("%d", &n);
    int k = n, res[100000], qian[100000], x;
    for(i = 0;i < n;i++){ 
    
        scanf("%d", &x);
        qian[x] = i;
    }
    while(k--){ 
    
        t = qian[m];
        res[t] = k;
        m = t;
    }
    for(i = 0;i < n;i++){ 
    
        if(i) printf(" ");
        printf("%d", res[i] + 1);
    }
    return 0;
}

原网站

版权声明
本文为[Python's path to immortality]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132041412669.html