PAT 乙级 1020 月饼
测试点2:库存不是整数
// 1020 月饼.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
struct Node{
double x;
double y;
double profit = x / y;
};
bool cmp(Node& n1, Node& n2) {
return n1.profit > n2.profit;
}
int main()
{
int n, d;
cin >> n >> d;
vector<Node> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].x;
}
for (int i = 0; i < n; i++) {
cin >> a[i].y;
a[i].profit = a[i].y / a[i].x;
}
sort(a.begin(), a.end(), cmp);
int i = 0;
double profit = 0;
while (d > 0 && i < n) {
if (d <= a[i].x) {
profit += d * a[i].profit;
d = 0;
}
else {
profit += a[i].x * a[i].profit;
d -= a[i].x;
i++;
}
}
cout << setiosflags(ios::fixed) << setprecision(2) << profit;
}
设置精度:
cout << setioflags(ios::fixed) << setprecision(2) << number;