-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknaps.cpp
More file actions
69 lines (69 loc) · 1.32 KB
/
knaps.cpp
File metadata and controls
69 lines (69 loc) · 1.32 KB
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
#include<stdio.h>
#include<conio.h>
void knapsack(float cap, int n, float w[], float val[])
{
float arr[20], maxv,y;
int i,j;
y=cap;
maxv=0;
for(i=0;i < n;i++)
arr[i]=0.0;
for(i=0;i < n;i++)
{
if(w[i] > y)
break;
else
{
arr[i]=1.0;
maxv=maxv+val[i];
y=y-w[i];
}
}
if(i < n)
arr[i]=y/w[i];
maxv=maxv+(arr[i]*val[i]);
printf("The selected elements are:-\n ");
for(i=0;i < n;i++)
if(arr[i]==1.0)
printf("\nval is %f with w %f ", val[i], w[i]);
else if(arr[i] > 0.0)
printf("\n%f part of val %f with w %f", arr[i], val[i], w[i]);
printf("\nTotal val for %d objects with cap %f = %f\n\n", n, cap,maxv);
}
int main()
{
float w[20],val[20],Pi[20], t1,t2,t3;
int n;
float cap;
int i,j;
printf("Enter number of objects: ");
scanf("%d", &n);
printf("\nEnter the cap of knapsack: ");
scanf("%f", &cap);
for(i=0;i < n;i++)
{
printf("\nEnter %d(th) val: ", (i+1));
scanf("%f", &val[i]);
printf("Enter %d(th) w: ", (i+1));
scanf("%f", &w[i]);
Pi[i]=val[i]/w[i];
}
for(i=0;i < n;i++)
for(j=0;j < n;j++)
{
if(Pi[i] > Pi[j])
{
t1=Pi[i];
Pi[i]=Pi[j];
Pi[j]=t1;
t2=w[i];
w[i]=w[j];
w[j]=t2;
t3=val[i];
val[i]=val[j];
val[j]=t3;
}
}
knapsack(cap,n,w,val);
return(0);
}