-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_3.cpp
More file actions
56 lines (44 loc) · 644 Bytes
/
4_3.cpp
File metadata and controls
56 lines (44 loc) · 644 Bytes
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
#include<iostream>
using namespace std;
int main()
{
float w[10],v[10],max_value,x[10];
int n;
cin>>n>>max_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(w[i]/v[i]>w[j]/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 (max_value>w[i])
{
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;
}