-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumOfSubset.c
52 lines (52 loc) · 1.07 KB
/
sumOfSubset.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>
int w[10], x[10], d;
void sum_subset(int s, int k, int r)
{
int i;
static int b = 1;
x[k] = 1;
if (s + w[k] == d)
{
printf("SUBSET %d:\n", b++);
for (i = 1; i <= k; i++)
{
if (x[i] == 1)
printf("%d\t", w[i]);
}
printf("\n");
}
else if (s + w[k] + w[k + 1] <= d)
sum_subset(s + w[k], k + 1, r - w[k]);
if ((s + r - w[k] >= d) && (s + w[k + 1] <= d))
{
x[k] = 0;
sum_subset(s, k + 1, r - w[k]);
}
if (b == 1)
{
printf("No solutions");
exit(0);
}
}
int main()
{
int n, i, sum = 0;
printf("Enter the number of elements:\n");
scanf("%d", &n);
printf("Enter %d elements in increasing order:\n", n);
for (i = 1; i <= n; i++)
{
scanf("%d", &w[i]);
sum += w[i];
}
printf("Read the subset max value:\n");
scanf("%d", &d);
if (sum < d || w[1] > d)
{
printf("No solutions\n");
exit(0);
}
sum_subset(0, 1, sum);
return 0;
}