mirror of
https://github.com/hao14293/2021-Postgraduate-408.git
synced 2026-02-03 02:23:40 +08:00
Create L2-003 月饼 (25 分).cpp
This commit is contained in:
37
GPLT/CPP/L2-003 月饼 (25 分).cpp
Normal file
37
GPLT/CPP/L2-003 月饼 (25 分).cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
struct mooncake{
|
||||
float mount, price, unit;
|
||||
};
|
||||
|
||||
int cmp(mooncake a, mooncake b){
|
||||
return a.unit > b.unit;
|
||||
}
|
||||
|
||||
int main(){
|
||||
int n, need;
|
||||
cin >> n >> need;
|
||||
vector<mooncake> a(n);
|
||||
for(int i = 0; i < n; i++)
|
||||
cin >> a[i].mount;
|
||||
for(int i = 0; i < n; i++)
|
||||
cin >> a[i].price;
|
||||
for(int i = 0; i < n; i++)
|
||||
a[i].unit = a[i].price / a[i].mount;
|
||||
sort(a.begin(), a.end(), cmp);
|
||||
float result = 0.0;
|
||||
for(int i = 0; i < n; i++){
|
||||
if(a[i].mount <= need){
|
||||
result = result + a[i].price;
|
||||
}else{
|
||||
result = result + a[i].unit * need;
|
||||
break;
|
||||
}
|
||||
need = need - a[i].mount;
|
||||
}
|
||||
printf("%.2f", result);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user