4 条题解

  • 0
    @ 2024-4-11 14:22:52

    #include<iostream> #include<vector> #include<algorithm> #include<iomanip> using namespace std; struct gold { double value; double weight; double rate; }; bool cmp(const gold& t1,const gold& t2) { return t1.rate > t2.rate; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, t; double numt = 0; double numv=0; vector<gold> v; cin >> n >> t; if (n < 0 || t < 0 || n>100 || t>1000)return 0; v.resize(n); for (int i = 0; i < n; i++) { cin >> v[i].weight >> v[i].value; if (v[i].value < 1 || v[i].weight < 1 || v[i].value>120)return 0; v[i].rate = v[i].value / v[i].weight; } sort(v.begin(),v.end(),cmp); int i = 0; while (numt <= t && i < n) { numt += v[i].weight;//可能会大于 numv += v[i].value; i++; } if (numt > t) { numv-=(numt - t)* v[i-1].rate; } cout << fixed<<setprecision(2)<<numv; return 0; }

    信息

    ID
    77
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    342
    已通过
    82
    上传者