結構_[CPE] Rockabye Tobby_終於對了==

 avatar
user_3763047219
c_cpp
2 years ago
1.5 kB
1
Indexable
Never
#include <stdio.h>
#include<stdlib.h>

struct data {
    char med[15];
    int time;
};

void freq(struct data medlist[], int k, int n, int time[]) {
    if (k != 0) {
        int min = medlist[0].time;
        int index = 0;
        for (int i = 0; i < n; i++) {
            if (min > medlist[i].time) {
                min = medlist[i].time;
                index = i;
            }
            else if (min == medlist[i].time) {
                if (i < index) {
                    min = medlist[i].time;
                    index = i;
                }
            }
        }
        if (k == 1) {
            printf("%d %s", medlist[index].time, medlist[index].med);
        }
        else {
            printf("%d %s", medlist[index].time, medlist[index].med);
            printf("\n");
        }

        medlist[index].time = medlist[index].time + time[index];
        freq(medlist, k - 1, n, time);
    }
}

int main() {
    int case1 = 0;
    scanf("%d", &case1);

    while (case1--) {
        int n = 0, k = 0;
        struct data* medlist;
        scanf("%d %d", &n, &k);
        medlist = (struct data*)malloc(sizeof(struct data) * n);
        for (int i = 0; i < n; i++) {
            scanf("%s %d", &medlist[i].med, &medlist[i].time);
        }
        int* time;
        time = (int*)malloc(sizeof(int) * n);
        for (int i = 0; i < n; i++) {
            time[i] = medlist[i].time;
        }
        freq(medlist, k, n, time);
    }

}