您的位置:首页 > 其它

PAT basic 1020

2017-08-02 13:07 162 查看
#include<iostream>
#include<algorithm>

using namespace std;

struct mooncake {
float mount;
float price;
float unit;
};

int cmp(mooncake a,mooncake b) {
return a.unit > b.unit;
}

int main() {
int n;
cin >> n;
int need;
cin >> need;

mooncake *a = new mooncake
;
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, a + n, cmp);

float result = 0.0;
for(int i = 0; i < n; i++) {
if(a[i].mount <= need) {
result += a[i].price;
} else {
result += a[i].unit * need;
break;
}
need -= a[i].mount;
}
//cout << result; 格式错误
printf("%.2f",result);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: