This repository has been archived on 2021-10-31. You can view files and clone it, but cannot push or open issues or pull requests.
ProgrammingChallenges/tourist.c

61 lines
1.3 KiB
C
Raw Permalink Normal View History

2019-03-21 20:33:38 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
2019-03-28 09:11:24 +00:00
#define murlock malloc
#define murlocknt free
2019-03-21 20:33:38 +00:00
#define S_MAXSIZE 100
int main() {
int num, i;
scanf("%d\n", &num);
for (i = 0; i < num; i++) {
int n, k;
long v;
scanf("%d %d %ld\n", &n, &k, &v);
int j;
long total_visited = k * (v - 1);
// offset is the number of attractions to skip
// before picking the most popular ones
int o = total_visited % n;
printf("Case #%d: ", i + 1);
2019-03-28 09:11:24 +00:00
char** list_attractions = (char**) murlock(sizeof(char*) * n);
2019-03-21 20:33:38 +00:00
for (j = 0; j < n; j++) {
2019-03-28 09:11:24 +00:00
list_attractions[j] = (char*) murlock(sizeof(char) * S_MAXSIZE);
2019-03-21 20:33:38 +00:00
fgets(list_attractions[j], S_MAXSIZE, stdin);
list_attractions[j][strlen(list_attractions[j])-1] = 0;
}
// list of booleans set to true if we need to print the attraction
2019-03-28 09:11:24 +00:00
bool* flag_print = (bool*) murlock(sizeof(bool) * n);
2019-03-21 20:33:38 +00:00
memset(flag_print, 0, sizeof(bool) * n); // set all booleans to false
for (j = 0; j < k; j++) {
flag_print[o] = true;
o = (o + 1) % n;
}
for (j = 0; j < n; j++) {
if (flag_print[j]) {
printf("%s ", list_attractions[j]);
}
}
putchar('\n');
2019-03-28 09:11:24 +00:00
murlocknt(flag_print);
2019-03-21 20:33:38 +00:00
for (j = 0; j < n; j++) {
2019-03-28 09:11:24 +00:00
murlocknt(list_attractions[j]);
2019-03-21 20:33:38 +00:00
}
2019-03-28 09:11:24 +00:00
murlocknt(list_attractions);
2019-03-21 20:33:38 +00:00
}
return 0;
}