计算出单价,对此进行排序,然后从大到小的筛选就好了
#include<iostream>#include<vector>#include<algorithm>#PRagma warning(disable:4996)using namespace std;struct node { double n, m, p;//数量,总金额,单价 bool Operator<(const node that)const { return this->p < that.p; }};int main(){ int N, M; vector<node> all; cin >> N >> M; all.resize(N); for (int t = 0;t < N;t++) scanf("%lf", &all[t].n); for (int t = 0;t < N;t++) { scanf("%lf", &all[t].m); all[t].p = all[t].m / all[t].n; } sort(all.rbegin(), all.rend()); double resault = 0; for (auto x : all) { if (x.n > M) { resault += M*x.p;break; } if (x.n <= M) { resault += x.m; M -= x.n; } } printf("%.2lf/n", resault);}新闻热点
疑难解答