50019. Medicine

I'm a slow walker, but I never walk backwards.

Problem Description

Write a program to process medicine. A medicine has up to 2 active ingredients, and up to 5 inactive ingredients. A ingredient has a name (up to 127 characters) and a weight (a non-negative integer no more than 5000). Now implement the following functions about a medicine.

  • void init(Medicine *medicine);
    Initialize the medicine so that it does not have any ingredients.
  • int addActive(Medicine *medicine, char *name, int weight);
    Add the weight of active ingredient of name by the given weight, and return the weight after addition. If we try to add a new active ingredient and the number of active ingredient is already 2, then do not add it and return -1 as warning.
  • int addInactive(Medicine *medicine, char *name, int weight);
    Add the weight of inactive ingredient of name by the given weight, and return the weight after addition. If we try to add a new inactive ingredient and the number of active ingredient is already 5, then do not add it and return -1 as warning.
  • void print(Medicine *medicine);
    Print active ingredients then inactive ingredients. You should print both in non-decreasing order of weight. If the two ingredients have the same weight then print the ingredients in dictionary order.
  • int totalWeight(Medicine *medicine);
    Return the total weight of this medicine.
  • char *maxIngredient(Medicine *medicine);
    Return the name of the ingredient that has the maximum weight. If there are two ingredients with the same weight, then return the one appears earlier in dictionary order. If there are no ingredients in the medicine, return NULL.

Subtask

  • 10pt. Implement init, addActive, and totalWeight. It is guaranteed that we will not add more than 2 active ingredients.
  • 15pt. Implement init, addActive, totalWeight, and maxIngredient. It is guaranteed that we will not add more than 2 active ingredients.
  • 15pt. Implement init, addActive, addInactive, totalWeight, and maxIngredient. It is guaranteed that we will not add more than 2 active ingredients or 5 inactive ingredients.
  • 30pt. Implement init, addActive, addInactive, and totalWeight. We may add more than 2 active ingredients or 5 inactive ingredients.
  • 30pt. Implement all functions including print.

main.c

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
#include "medicine.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
 
void test_all() {
    int n, m;
    scanf("%d %d", &n, &m);
 
    Medicine *medi = (Medicine *) malloc(sizeof(Medicine) * n);
    int cmd, mid, weight;
    char name[128];
    for (int i = 0; i < n; i++)
        init(&medi[i]);
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &cmd, &mid);
        if (cmd == 1) {         // addActive
            scanf("%s %d", name, &weight);
            int af = addActive(&medi[mid], name, weight);
            printf("af %d\n", af);
        } else if (cmd == 2) {    // addInactive
            scanf("%s %d", name, &weight);
            int bf = addInactive(&medi[mid], name, weight);
            printf("bf %d\n", bf);
        } else if (cmd == 3) {    // print
            print(&medi[mid]);
        } else if (cmd == 4) {    // totalWeight
            printf("weight = %d\n", totalWeight(&medi[mid]));
        } else if (cmd == 5) {    // maxIngredient
            char *ret = maxIngredient(&medi[mid]);
            printf("main ingredient = %s\n", ret == NULL ? "NOT FOUND" : ret);
        }
    }
}
int main() {
    test_all();
    return 0;
}

Sample Input

2 32
1 1 light 10
4 1
5 1
1 1 land 90
5 1
3 1
4 0
1 0 pangfeng 98
4 0
5 0
1 0 pangfeng 1
4 0
1 0 struggle 0
1 0 struggle 0
4 0
1 0 struggle 1
4 0
1 0 struggle 0
3 0
4 0
2 0 morris 1
2 0 fool 1
5 0
2 0 fool 2
5 0
2 0 morris 3
3 0
4 0
3 1
4 1
5 0
5 1

Sample Output

af 10
weight = 10
main ingredient = light
af 90
main ingredient = land
----- Active Ingredient begin   -----
90 land
10 light
----- end                       -----
----- Inactive Ingredient begin -----
----- end                       -----
weight = 0
af 98
weight = 98
main ingredient = pangfeng
af 99
weight = 99
af 0
af 0
weight = 99
af 1
weight = 100
af 1
----- Active Ingredient begin   -----
99 pangfeng
1 struggle
----- end                       -----
----- Inactive Ingredient begin -----
----- end                       -----
weight = 100
bf 1
bf 1
main ingredient = pangfeng
bf 3
main ingredient = pangfeng
bf 4
----- Active Ingredient begin   -----
99 pangfeng
1 struggle
----- end                       -----
----- Inactive Ingredient begin -----
4 morris
3 fool
----- end                       -----
weight = 107
----- Active Ingredient begin   -----
90 land
10 light
----- end                       -----
----- Inactive Ingredient begin -----
----- end                       -----
weight = 100
main ingredient = pangfeng
main ingredient = land

Discussion