計算出單價,對此進行排序,然后從大到小的篩選就好了
#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);}新聞熱點
疑難解答