当前位置:网站首页>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 )
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 )
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 )

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 .
#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 )

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 )
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;
}
边栏推荐
- 备忘一下jvxetable的各种数据集获取方法
- YYGH-11-定时统计
- LeetCode_ String inversion_ Simple_ 557. Reverse word III in string
- Notes, continuation, escape and other symbols
- Vulhub vulnerability recurrence 68_ ThinkPHP
- Auto.js学习笔记17:基础监听事件和UI简单的点击事件操作
- PDK工艺库安装-CSMC
- Check the useful photo lossless magnification software on Apple computer
- Summary of deep learning tuning tricks
- Station B, Master Liu Er - back propagation
猜你喜欢
![[cloud native] 3.1 kubernetes platform installation kubespher](/img/86/137a65a5b58bc32e596d2a330ca9fc.png)
[cloud native] 3.1 kubernetes platform installation kubespher

嵌入式面试题(四、常见算法)

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

【torch】|torch.nn.utils.clip_grad_norm_

Redis message queue

Yygh-11-timing statistics

How to use PHP string query function

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
[email protected] raspberry pie"/>[email protected] raspberry pie

Safe mode on Windows
随机推荐
【torch】|torch. nn. utils. clip_ grad_ norm_
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
ArcGIS应用基础4 专题图的制作
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
自建DNS服务器,客户端打开网页慢,解决办法
【SQL server速成之路】——身份驗證及建立和管理用戶賬戶
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
Problems encountered in installing mysql8 on MAC
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
What preparations should be made for website server migration?
UCF (2022 summer team competition I)
进程和线程
Download, install and use NVM of node, and related use of node and NRM
01. Project introduction of blog development project
[machine learning notes] univariate linear regression principle, formula and code implementation
PDK工艺库安装-CSMC
Embedded interview questions (IV. common algorithms)
[detailed explanation of Huawei machine test] statistics of shooting competition results
网站进行服务器迁移前应做好哪些准备?