-
Notifications
You must be signed in to change notification settings - Fork 0
/
17b.c
58 lines (51 loc) · 1.22 KB
/
17b.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define AMOUNT 150
int *containers;
int minFilledContainers = AMOUNT;
int possibilities = 0;
void doLoop(int currentAmount, int pos, int filledContainers, int n)
{
if (pos == n)
{
if (currentAmount == AMOUNT && filledContainers < minFilledContainers)
{
possibilities = 1;
minFilledContainers = filledContainers;
}
else if (currentAmount == AMOUNT && filledContainers == minFilledContainers)
possibilities++;
}
else
{
doLoop(currentAmount, pos + 1, filledContainers, n); /* don't fill current container */
doLoop(currentAmount + containers[pos], pos + 1, filledContainers + 1, n); /* fill current container */
}
}
int main(void)
{
int amount = 0;
containers = malloc(sizeof(int));
if (!containers)
{
fprintf(stderr, "malloc fail\n");
exit(-1);
}
char line[sizeof(int)];
while(scanf("%s", line) != EOF)
{
amount++;
containers = realloc(containers, (unsigned)amount * sizeof(int));
if (!containers)
{
fprintf(stderr, "realloc fail\n");
exit(-1);
}
containers[amount-1] = atoi(line);
}
doLoop(0, 0, 0, amount);
printf("possibilities: %d\n", possibilities);
return 0;
}