-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_1.cpp
59 lines (48 loc) · 747 Bytes
/
4_1.cpp
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
#include<iostream>
using namespace std;
int main()
{
float w[10],v[10],max_value=0;int n;
float x[10];
cout<<"number of items";
cin>>n;
cout<<"enter the max_size_of_mem";
cin>>max_value;
cout<<"enter weight -- value";
for (int i = 0; i < n; ++i)
cin>>w[i]>>v[i];
for (int i = 0; i < n; ++i)
{
for (int j = i+1; j < n; ++j)
{
if(v[i]>v[j])
{
int temp = v[i];
v[i]=v[j];
v[j] = temp;
temp = w[i];
w[i]=w[j];
w[j]=temp;
}
}
}
for (int i = 0; i < n; ++i)
{
if (w[i]<max_value)
{
x[i] = 1.0;
max_value-=w[i];
}
else
{
x[i] = max_value/w[i];
max_value=0;
}
}
float max_profit = 0;
for (int i = 0; i < n; ++i)
{
max_profit+=x[i]*v[i];
}
cout<<max_profit;
}