-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubset_equal_n.cpp
More file actions
40 lines (37 loc) · 935 Bytes
/
subset_equal_n.cpp
File metadata and controls
40 lines (37 loc) · 935 Bytes
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
#include <stdlib.h>
#include <stdio.h>
#include <list>
/*this is problem about the 0-1 bag
*find the subset of a set added up to a sum
*/
using namespace std;
list<int> all;
void subset(int sum, int num);
int main()
{
int sum,num;
printf("Please input the sum:\n");
scanf("%d", &sum);
printf("Please input the num:\n");
scanf("%d", &num);
subset(sum, num);
return 0;
}
void subset(int sum, int num)
{
if (sum <= 0 || num <= 0)
return;
if (sum == num) {
all.reverse();
list<int>::iterator it = all.begin();
for (; it!=all.end(); it++) {
printf("%d + ", *it);
}
printf("%d\n", num);
all.reverse();
}
all.push_front(num);
subset(sum-num, num-1);
all.pop_front();
subset(sum, num-1);
}