-
Notifications
You must be signed in to change notification settings - Fork 6
/
knapsack.C
81 lines (81 loc) · 1.09 KB
/
knapsack.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,t=0,t1=0,t2=0,t3=0,W=0,k=0;
int C[5],w[5],L[5],I[]={1,2,3,4,5,6};
clrscr();
printf("\nEnter the total capacity\n");
scanf("%d",&k);
printf("\n Enter each item cost \n");
for(i=0;i<5;i++)
{
scanf("%d",&C[i]);
}
printf("\n Enter the item weight\n");
for(i=0;i<5;i++)
{
scanf("%d",&w[i]);
}
printf("\n The input data is");
printf("\n Item No\tCost\tWeight");
printf("\n......\t........\t........\n");
for(i=0;i<5;i++)
{
printf("%d\t\t%d\t\t%d",I[i],C[i],w[i]);
printf("\n");
}
for(i=0;i<5;i++)
{
L[i]=C[i]/w[i];
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(L[i]<L[j])
{
t=L[i];
L[i]=L[j];
L[j]=t;
t1=C[i];
C[i]=C[j];
C[j]=t1;
t2=w[i];
w[i]=w[j];
w[j]=t2;
t3=I[i];
I[i]=I[j];
I[j]=t3;
}
}
}
printf("\n The arranged data is");
printf("\n Item No\tCost\tWeight");
printf("\n......\t.........\t.........\n");
for(i=0;i<5;i++)
{
printf("%d\t\t%d\t\t%d",I[i],C[i],w[i]);
printf("\n");
}
for(i=0;i<5;i++)
{
if(w[i]<k)
{
W=W+C[i];
k=k-w[i];
}
else if(w[i]>k && k>0)
{
W=W+L[i]*k;
k=0;
}
else if(k==0)
{
break;
}
}
printf("\n The total cost is = %d",W);
getch();
return 0;
}