-
Notifications
You must be signed in to change notification settings - Fork 0
/
17a.c
52 lines (45 loc) · 960 Bytes
/
17a.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define AMOUNT 150
int *containers;
int possibilities = 0;
void doLoop(int currentAmount, int pos, int n)
{
if (pos == n)
{
if (currentAmount == AMOUNT)
possibilities++;
}
else
{
doLoop(currentAmount, pos + 1, n); /* don't fill current container */
doLoop(currentAmount + containers[pos], pos + 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, amount);
printf("possibilities: %d\n", possibilities);
return 0;
}