当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
01. 开发博客项目之项目介绍
JS array list actual use summary
[Jiudu OJ 08] simple search x
[force buckle]43 String multiplication
实践分享:如何安全快速地从 Centos迁移到openEuler
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
[Tang Laoshi] C -- encapsulation: classes and objects
Unity Vector3. Use and calculation principle of reflect
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
随机推荐
What preparations should be made for website server migration?
备忘一下jvxetable的各种数据集获取方法
初识数据库
Vulhub vulnerability recurrence 68_ ThinkPHP
Jushan database appears again in the gold fair to jointly build a new era of digital economy
PDK工艺库安装-CSMC
Pytorch代码注意的细节,容易敲错的地方
Self built DNS server, the client opens the web page slowly, the solution
Graduation design game mall
First acquaintance with CDN
02. Develop data storage of blog project
Quantitative description of ANC noise reduction
Unity Vector3. Use and calculation principle of reflect
应用安全系列之三十七:日志注入
指針經典筆試題
29io stream, byte output stream continue write line feed
Solution of QT TCP packet sticking
Station B, Master Liu Er - back propagation
UCF (2022 summer team competition I)
[Jiudu OJ 07] folding basket