#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; struct cake { double num = 0; double sale = 0; double pns = 0; } moon[1010]; bool cmp(cake a, cake b) { return (a.pns - b.pns) > 0; } int main(int argc, char *argv[]) { int n = 0; double s = 0; double total = 0; scanf("%d%lf", &n, &s); for (int i = 1; i <= n; i++) { scanf("%lf", &moon[i].num); } for (int i = 1; i <= n; i++) { scanf("%lf", &moon[i].sale); moon[i].pns = moon[i].sale / moon[i].num; } sort(moon + 1, moon + n + 1, cmp); for (int j = 1; j <= n; j++) { if (s < moon[j].num) { total += s * moon[j].pns; break; } else if (s >= moon[j].num) { total += moon[j].sale; s -= moon[j].num; } } printf("%.2f", total); return 0; }简化代码
时间: 2023-07-16 09:12:58 浏览: 353
duojikongzhi.rar_Unsigned_c++include <reg51>
以下是简化后的代码:
```c++
#include <iostream>
#include <algorithm>
using namespace std;
struct cake {
double num, sale, pns;
} moon[1010];
bool cmp(cake a, cake b) {
return (a.pns - b.pns) > 0;
}
int main() {
int n;
double s, total = 0;
cin >> n >> s;
for (int i = 1; i <= n; i++) {
cin >> moon[i].num;
}
for (int i = 1; i <= n; i++) {
cin >> moon[i].sale;
moon[i].pns = moon[i].sale / moon[i].num;
}
sort(moon + 1, moon + n + 1, cmp);
for (int j = 1; j <= n; j++) {
if (s < moon[j].num) {
total += s * moon[j].pns;
break;
} else {
total += moon[j].sale;
s -= moon[j].num;
}
}
printf("%.2f", total);
return 0;
}
```
阅读全文