50019. Medicine
難度:3.5/5
不難,與前一題相似。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109/*
typedef struct Ingredient{
int weight;
char name[128];
} Ingredient;
typedef struct Medicine{
int cnt_active;
Ingredient active[2];
int cnt_inactive;
Ingredient inactive[5];
} Medicine;
*/
#include "medicine.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmp(const void* a, const void *b){
Ingredient* ptr_a = (Ingredient*)a;
Ingredient* ptr_b = (Ingredient*)b;
if(ptr_a->weight < ptr_b->weight) return 1;
else if(ptr_a->weight > ptr_b->weight) return -1;
else{
return strcmp(ptr_a->name, ptr_b->name);
}
}
void init(Medicine *med){
med->cnt_active = 0;
med->cnt_inactive = 0;
}
int addActive(Medicine *med, char *name, int weight){
for(int i = 0; i < med->cnt_active; i++){ // existing
if(!strcmp(med->active[i].name, name)){
med->active[i].weight += weight;
return med->active[i].weight;
}
}
if(med->cnt_active == 2) return -1; // add new but full
strcpy(med->active[med->cnt_active].name, name); // add new but not full
med->active[med->cnt_active].weight = weight;
med->cnt_active += 1;
return weight;
}
int addInactive(Medicine *med, char *name, int weight){
for(int i = 0; i < med->cnt_inactive; i++){ // existing
if(!strcmp(med->inactive[i].name, name)){
med->inactive[i].weight += weight;
return med->inactive[i].weight;
}
}
if(med->cnt_inactive == 5) return -1; // add new but full
strcpy(med->inactive[med->cnt_inactive].name, name); // add new but not full
med->inactive[med->cnt_inactive].weight = weight;
med->cnt_inactive += 1;
return weight;
}
void print(Medicine *med){
qsort(med->active, med->cnt_active, sizeof(Ingredient), cmp);
qsort(med->inactive, med->cnt_inactive, sizeof(Ingredient), cmp);
printf("----- Active Ingredient begin -----\n");
for(int i = 0; i < med->cnt_active; i++){
printf("%d %s\n", med->active[i].weight, med->active[i].name);
}
printf("----- end -----\n");
printf("----- Inactive Ingredient begin -----\n");
for(int i = 0; i < med->cnt_inactive; i++){
printf("%d %s\n", med->inactive[i].weight, med->inactive[i].name);
}
printf("----- end -----\n");
}
int totalWeight(Medicine *med){
int ans = 0;
for(int i = 0; i < med->cnt_active; i++){
ans += med->active[i].weight;
}
for(int i = 0; i < med->cnt_inactive; i++){
ans += med->inactive[i].weight;
}
return ans;
}
char *maxIngredient(Medicine *med){
qsort(med->active, med->cnt_active, sizeof(Ingredient), cmp);
qsort(med->inactive, med->cnt_inactive, sizeof(Ingredient), cmp);
if(med->cnt_active == 0 && med->cnt_inactive == 0) return NULL;
else if(med->cnt_active == 0) return med->inactive[0].name;
else if(med->cnt_inactive == 0) return med->active[0].name;
else{
int c = cmp((void*)(&med->active[0]), (void*)(&med->inactive[0]));
if(c < 0) return med->active[0].name;
else return med->inactive[0].name;
}
}