結構_[CPE] Rockabye Tobby
user_3763047219
c_cpp
3 years ago
1.5 kB
3
Indexable
#include <stdio.h> #include<stdlib.h> struct data { char med[15]; int time; }; int main() { int case1 = 0; scanf("%d", &case1); for (int j = 0; j < case1; j++) { 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; } int min = medlist[0].time; int index = 0; while (k > 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]; k--; } } }
Editor is loading...