-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabfinal.cpp
More file actions
65 lines (50 loc) · 1023 Bytes
/
labfinal.cpp
File metadata and controls
65 lines (50 loc) · 1023 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
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
#include <iostream>
using namespace std;
int n, d, w[10], x[10], count=0;
void subset(int cs, int k, int r)
{
int i;
x[k] = 1;
if(cs + w[k] == d)
{
cout<< "\n\nSolution " << ++count << " is:";
for(i=0; i<=k; i++){
if(x[i] == 1){
cout << w[i] << " ";
}
}
}
else if(cs + w[k] + w[k+1] <= d)
{
subset(cs + w[k], k+1, r - w[k]);
}
if(cs + w[k+1] <= d && cs + r - w[k] >= d)
{
x[k] = 0;
subset(cs, k+1, r-w[k]);
}
}
int main()
{
int sum=0, i;
cout << "enter the input elements number "<<"\n";
cin >> n;
cout <<"enter " << n << " elements\n";
for(i=0; i<n; i++)
{
cin >> w[i];
}
for(i=0; i<n; i++){
sum =sum + w[i];
}
cout << "enter value of sum " <<"\n";
cin >> d;
if(sum < d)
{
cout<<"INVALID SOLUTION\n";
}
else
{
subset(0,0,sum);
}
}