思路:
本题考察了贪心的思想,不买对的,就买贵的。
先按照单价进行排序,在德才论中,我们已经会用qsort来给struct类型排序了,那么就用起来。
注意这里的库存量也是double类型的,不要被样例欺骗了。
代码:
#include<iostream>
#include<cstdlib>
#include<iomanip>
using namespace std;
struct Objc
{
double num;
double total;
double upr;
}th[1002];
int n;
double d;
int cmp(const void *a, const void *b)
{
struct Objc *aa = (Objc *)a;
struct Objc *bb = (Objc *)b;
if (aa->upr < bb->upr)
return 1;
else return -1;
}
int main()
{
int i;
int count = 0;
double ans = 0;
cin >> n >> d;
for (i = 0; i < n; i++)
cin >> th[i].num;
for (i = 0; i < n; i++)
{
cin >> th[i].total;
th[i].upr = th[i].total / th[i].num;
}
qsort(th, n, sizeof(th[0]), cmp);
while (d > 0 && count < n)
{
if (th[count].num > d)
{
ans = ans + d*th[count].upr;
d = 0;
continue;
}
ans += th[count].total;
d -= th[count].num;
count++;
}
cout.setf(ios::fixed);
cout << fixed << setprecision(2) << ans << endl;
//while (1)
//{
//}
return 0;
}