-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14.cpp
More file actions
41 lines (37 loc) · 764 Bytes
/
14.cpp
File metadata and controls
41 lines (37 loc) · 764 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
#include <iostream>
using namespace std;
int main()
{
int n;
float m;
cout << "enter n,w" << endl;
cin >>n;
cin >>m;
float *w=new float[n];
int *c=new int[n];
for (int i=0;i<n;i++){
w[i]=rand();
c[i]=rand();
cout <<w[i]<<" "<<c[i]<<endl;
}
for (int i=0;i<n;i++){
for (int k=0;k<n-1;k++){
if (c[k]/w[k]<c[k+1]/w[k+1]){
swap(c[k],c[k+1]);
swap(w[k],w[k+1]);
}
}
}
float s=0;
for (int i=0;i<n;i++){
if (m>w[i]){
m-=w[i];
s+=c[i];
}
else{
s+=m*(c[i]/w[i]);
break;
}
}
cout <<s<<endl;
}