-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractional knapsack.c
66 lines (63 loc) · 1.78 KB
/
fractional 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
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
typedef struct{
int p,w;
float p_by_w;
float x;
int id;
}items;
int main(){
int i,n,m,j,k;
float cw=0;
float cp = 0;
printf("Enter the no of items");
scanf("%d",&n);
printf("Enter the sack capacity");
scanf("%d",&m);
items item[100],temp;
for(i=0;i<n;i++){
printf("enter profit,weight");
scanf("%d%d", &item[i].p,&item[i].w);
item[i].p_by_w = item[i].p*1.0/item[i].w ;
item[i].id = i;
}
//sorting on p/w ratio
for(i=1;i<n;i++){
for(j=0;j<n-i;j++){
if( item[j].p_by_w < item[j+1].p_by_w ){
temp = item[j];
item[j] = item[j+1];
item[j+1] = temp;
}
}
}
for(i=0;i<n;i++){
if( (cw + item[i].w) < m){
cp = cp + item[i].p;
cw = cw + item[i].w;
item[i].x = 1;
}
else{
item[i].x = (m - cw)*1.0/item[i].w ;
cp = cp +(item[i].p)*item[i].x;
cw = m;
break;
}
}
//now sort by item id to display
for(i=1;i<n;i++){
for(j=0;j<n-i;j++){
if( item[j].id > item[j+1].id ){
temp = item[j];
item[j] = item[j+1];
item[j+1] = temp;
}
}
}
printf("ID\tP\tW\tProfit/Weight\tX[i]\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\t%f\t%f\n",item[i].id,item[i].p,item[i].w,item[i].p_by_w,item[i].x);
printf("Profit=%f Weight=%f",cp,cw);
return 0;
}