---
# System prepended metadata

title: Lab3 參考解答

---

# Lab3 參考解答

# All you need is permutation
> Author: 111550139
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
  int testCase;
  int n, k;
  char **ingredient;
  int *frequency;
  scanf("%d", &testCase);
  while (testCase--) {
    scanf("%d %d", &n, &k); //n ingredients, k frequency
    // Write your code here
    int i, j;
    ingredient=(char**)malloc(n*sizeof(char*));
    for(i=0; i<n; i++) ingredient[i]=(char*)malloc(10000*sizeof(char));
    frequency=(int*)malloc(n*sizeof(int));
    char s[10000];
    int a;
    for(i=0; i<n; i++){
        scanf("%s", s);
        scanf("%d", &a);
        for(j=0; j<strlen(s)+1; j++) ingredient[i][j]=s[j]; 
        frequency[i]=a;
    }
    int time=1;
    j=0;
    while(j<k){
    	for(i=0; i<n; i++){
    		if(time%frequency[i]==0){
				printf("%d ", time);
    			printf("%s\n", ingredient[i]);
    			j++;
    			if(j>=k) break;
			}
		}
		time++;
	}
    for(i=0; i<n; i++) free(ingredient[i]);
    free(ingredient);
    free(frequency);
  }
  return 0;
}
```
