Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
332658 | 黄浙峰老师 | 【C6-7】阿里巴巴与四十大盗 | C++ | 通过 | 100 | 1 MS | 268 KB | 703 | 2025-07-15 09:43:11 |
//755 - 【C6-7】阿里巴巴与四十大盗 #include<bits/stdc++.h> using namespace std; struct Tr{ int w,v; double k; //性价比 }; bool cmp(Tr t1, Tr t2){ return t1.k>t2.k; }//根据性价比从大到小排序 int main(){ Tr t[110]; int n,c; cin>>n>>c; for(int i=1; i<=n; i++){ cin>>t[i].w; cin>>t[i].v; t[i].k=1.0*t[i].v/t[i].w; //计算性价比 } sort(t+1, t+n+1,cmp); int i=0; double s=0; while(c>0){ i++; if(c>=t[i].w){ //如果能整个装入背包 c=c-t[i].w;//装入背包重量减少 s=s+t[i].v;//获得价值 }else{ //否则分割 s+=c*t[i].k; c=0; } } cout<<fixed<<setprecision(1)<<s; return 0; }