代码实现:
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
double stock, price;
}goods;
int main()
{
int N, D;
goods *cargo;
scanf("%d %d", &N, &D);
cargo = (goods*)malloc(sizeof(goods)*N);
for (int i = 0; i < N; i++)scanf("%lf", &cargo[i].stock);
for (int i = 0; i < N; i++)
{
scanf("%lf", &cargo[i].price); cargo[i].price /= cargo[i].stock;
}
for (int i = 0; i < N; i++)
{
int index = i;
goods temp;
for (int j = i; j < N; j++)if (cargo[index].price < cargo[j].price)index = j;
temp = cargo[i]; cargo[i] = cargo[index]; cargo[index] = temp;
}
int cnt = 0; double get = 0;
do
{
if (cargo[cnt].stock >= D)
{
get += D * cargo[cnt].price;
D = 0;
}
else if (cargo[cnt].stock < D)
{
get += cargo[cnt].stock*cargo[cnt].price;
D -= cargo[cnt].stock;
cnt++;
}
} while (D != 0 && cnt != N);
printf("%.2f", get);
return 0;
}