-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quiz2.c
66 lines (63 loc) · 1.01 KB
/
Quiz2.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
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdbool.h>
int sumsTo(uint x[], uint n, uint k, uint v)
{
if(n == 0)
{
return false;
}
if(k == 0)
{
if(v == 0)
{
return true;
}
return false;
}
int res1 = 0;
if(v >= x[0])
{
res1 = sumsTo(x+1, n-1, k-1, v-x[0]);
}
int res2 = sumsTo(x+1, n-1, k, v);
return res1 + res2 > 0;
}
int main()
{
int one[7] = {0,1,2,3,4,5,6};
int two[3] = {4,8,1};
int three[15] = {4,15,65,34,7,2,74,2,6,8,2,45,724,12,4};
int four[0] = {};
if(sumsTo(one, 7, 3, 5))
{
printf("true\n");
}
else
{
printf("false\n");
}
if(sumsTo(two, 3, 3, 15))
{
printf("true\n");
}
else
{
printf("false\n");
}
if(sumsTo(three, 15, 2, 10))
{
printf("true\n");
}
else
{
printf("false\n");
}
if(sumsTo(four, 0, 3, 5))
{
printf("true\n");
}
else
{
printf("false\n");
}
}