-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.c
64 lines (60 loc) · 1.41 KB
/
9.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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int w[10],p[10],i,j,cap,n;
void main()
{
printf("enter the capacity : \n");
scanf("%d",&cap);
printf("enter the number of objects : \n");
scanf("%d",&n);
int l[n+1][cap+1];
bool selected[n+1][cap+1];
printf("enter the weights and its profits (increasing order of weight) :\n");
for(i=1;i<=n;i++)
{
scanf("%d%d",&w[i],&p[i]);
}
for(i=0;i<=n;i++)
{
for(j=0;j<=cap;j++)
{
if(i==0||j==0)
{
l[i][j]=0;
selected[i][j]=false;
}
else if (w[i]<=j)
{
if(p[i]+l[i-1][j-w[i]]>l[i-1][j])
{
l[i][j]=p[i]+l[i-1][j-w[i]];
selected[i][j]=true;
}
else
{
l[i][j]=l[i-1][j];
selected[i][j]=false;
}
}
else
{
l[i][j]=l[i-1][j];
selected[i][j]=false;
}
}
}
i=n;
j=cap;
printf("the objects are : \n");
while(i>0 && j>0)
{
if(selected[i][j])
{
printf("the selected weight is : %d and its profit is : %d \n",w[i],p[i]);
j=j-w[i];
}
i--;
}
printf("the max profit is : %d \n",l[n][cap]);
}