-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfractionalknapsack.c
More file actions
75 lines (72 loc) · 1.24 KB
/
fractionalknapsack.c
File metadata and controls
75 lines (72 loc) · 1.24 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
70
71
72
73
74
75
#include<stdio.h>
#include<stdlib.h>
int min(int x,int y);
int main()
{
int **arr,i,n,j,w,temp1,temp2,temp3,amount,cost=0;
printf("Enter the no of items : ");
scanf("%d",&n);
arr=(int**)calloc(n,sizeof(int*));
for(i=0;i<n;i++)
*(arr+i)=(int*)calloc(3,sizeof(int));
printf("Enter the item price :");
for(i=0;i<n;i++)
scanf("%d",(*(arr+i)+0));
printf("Enter the item quantity :");
for(i=0;i<n;i++)
{
scanf("%d",(*(arr+i)+1));
*(*(arr+i)+2)=*(*(arr+i)+0)/(*(*(arr+i)+1));
}
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
printf("%d ",*(*(arr+i)+j));
printf("\n");
}
printf("Enter the weight of bag : ");
scanf("%d",&w);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(*(*(arr+i)+2)<*(*(arr+j)+2))
{
temp1=*(*(arr+i)+0);
temp2=*(*(arr+i)+1);
temp3=*(*(arr+i)+2);
*(*(arr+i)+0)=*(*(arr+j)+0);
*(*(arr+i)+1)=*(*(arr+j)+1);
*(*(arr+i)+2)=*(*(arr+j)+2);
*(*(arr+j)+0)=temp1;
*(*(arr+j)+1)=temp2;
*(*(arr+j)+2)=temp3;
}
}
}
i=0;
while(w>0)
{
amount=min(w,*(*(arr+i)+1));
w=w-amount;
cost+=amount*(*(*(arr+i)+2));
i++;
}
printf("Cost = %d",cost);
/*5
10
20
30
40
30
20
100
90
160*/
}
int min(int x ,int y)
{
if(x>y)
return y;
else return x;
}