-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2.cpp
77 lines (73 loc) · 1.48 KB
/
p2.cpp
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
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
using namespace std;
int i,j;
int stepcount = 0;
int returnMAX(int p[],int l,int k)
{
int max = 0;stepcount++;
//finding max profit item index
for (i = k; i < l; i++)
{
stepcount++;
stepcount++;
if (p[i] > p[max])
{ stepcount++;
max = i;
stepcount++;
}
}stepcount++;
stepcount++;
return max;
}
//greedy on profit knapsack
void knapsack(int p[],int w[],int capacity,int l)
{
int index;
int x[l];
for (i = 0; i < l; i++)
{stepcount++;
x[i] = 0;
stepcount++;}
stepcount++;
for (j = 0; j < l && capacity > 0; j++)
{
stepcount++;
index = returnMAX(p,l,j);stepcount++;
stepcount++;
if (w[index] <= capacity)
{ stepcount++;
x[index] = 1;stepcount++;
capacity -= w[index];stepcount++;
}
else continue;
}stepcount++;
cout << "\nThe obtained solution (feasible) is: ";stepcount++;
for (i = 0; i < l; i++)
{ stepcount++;
cout << x[i] << " ";
stepcount++;
}
stepcount++;
}
int main()
{
int p[20],w[20],l,capacity;
cout << "\nEnter the number of items: ";stepcount++;
cin >> l;stepcount++;
cout << "\nEnter the weight of these items: ";stepcount++;
for (i = 0; i < l; i++)
{ stepcount++;
cin >> w[i];
}stepcount++;
cout << "\nEnter individual profit: ";stepcount++;
for (j = 0; j < l; j++)
{ stepcount++;
cin >> p[i];
stepcount++;
}stepcount++;
cout << "\nEnter capacity of the knapsack: ";stepcount++;
cin >> capacity;stepcount++;
knapsack(p,w,capacity,l);stepcount++;
stepcount++;
return stepcount;
}