16 条题解
-
12
金币Lv.1
1.课听了吧?
- 听了,这道题是作业
- 没听
直接把作业拷过来就行
#include <iostream> #include <algorithm> #include <iomanip> using namespace std; struct Coin { double m,v,avg; } a[10005]; bool cmp(Coin a,Coin b) { return a.avg > b.avg; } int main() { int n, t; cin >> n >> t; for (int i = 0; i < n; i++) { cin >> a[i].m >> a[i].v; a[i].avg = a[i].v/a[i].m; } sort(a, a + n, cmp); int pos = 0; double sum = 0; while (t > 0) { double temp; if(t > a[pos].m) temp = a[pos].m; else temp = t; sum += temp * a[pos].avg,t -= temp; pos++; } cout << fixed << setprecision(2) << sum; return 0; }
hetao8554411 20230712
-
7
#include <iostream> #include <algorithm> #include <iomanip> using namespace std; struct Coin { double m,v,avg; } a[10005]; bool cmp(Coin a,Coin b) { return a.avg > b.avg; } int main() { int n, t; cin >> n >> t; for (int i = 0; i < n; i++) { cin >> a[i].m >> a[i].v; a[i].avg = a[i].v/a[i].m; } sort(a, a + n, cmp); int pos = 0; double sum = 0; while (t > 0) { double temp; if(t > a[pos].m) temp = a[pos].m; else temp = t; sum += temp * a[pos].avg,t -= temp; pos++; } cout << fixed << setprecision(2) << sum; return 0; }
没上课:好难的部分背包 上课了:简单的部分背包
-
5
#include <iostream>//hetao1357085 #include <algorithm> #include <iomanip> using namespace std; struct Coin { double m,v,avg; } a[10005]; bool cmp(Coin a,Coin b) { return a.avg > b.avg; } int main() { int n, t; cin >> n >> t; for (int i = 0; i < n; i++) { cin >> a[i].m >> a[i].v; a[i].avg = a[i].v/a[i].m; } sort(a, a + n, cmp); int pos = 0; double sum = 0; while (t > 0) { double temp; if(t > a[pos].m) temp = a[pos].m; else temp = t; sum += temp * a[pos].avg,t -= temp; pos++; } cout << fixed << setprecision(2) << sum; return 0; }
Jntm,给个赞吧
-
4
e.....这道题怎么说呢,说是难题吧你上了课就秒变签到题,说是签到题吧,你不上课就是挺难的部分背包><
```#include <bits/stdc++.h> using namespace std; struct Crystal { double volume, power; double avg; }; Crystal a[10005]; bool cmp(Crystal a, Crystal b) { return a.avg > b.avg; } int main() { int n, v; cin >> n >> v; for (int i = 0; i < n; i++) { cin >> a[i].volume >> a[i].power; a[i].avg = a[i].power/a[i].volume; } sort(a,a+n,cmp ); int pos = 0; double sum = 0; while (v > 0) { double temp; if(v > a[pos].volume) temp = a[pos].volume; else temp = v; sum+=temp*a[pos].avg; v-=temp; pos++; } cout << fixed << setprecision(2) << sum << endl;//保留两位小数输出,setprecision(2)的“2”是可以更改的 return 0; }
-
2
题目大意
存在n堆金币,第i堆金币的总重量和总价值是mi, vi。存在承重为T的背包,金币可切割,问最多可以拿走多少价值的金币。
完整思路
将每堆金币的单位价值求出来,并按照单位价值从大到小排序,一个个加入到背包中。
核心代码
//对单位价值排序 struct node { // 定义结构体 double w; // 重量 double v; // 价值 double p; // 性价比 }; bool cmp(node a, node b) { return a.p > b.p; // 性价比从大到小排序 }
//加入到背包中 int pos = 0; double sum = 0; while (c > 0) { double temp = min(a[pos].w, c); c -= temp; sum += temp * a[pos].p; pos++; }
-
1
cpp
#include <iostream> #include <algorithm> #include <iomanip>
using namespace std;
struct Coin { double m,v,avg; } a[10005];
bool cmp(Coin a,Coin b) { return a.avg > b.avg; }
int main() { int n, t; cin >> n >> t; for (int i = 0; i < n; i++) { cin >> a[i].m >> a[i].v; a[i].avg = a[i].v/a[i].m; }
sort(a, a + n, cmp); int pos = 0; double sum = 0; while (t > 0) { double temp; if(t > a[pos].m) temp = a[pos].m; else temp = t; sum += temp * a[pos].avg,t -= temp; pos++; } cout << fixed << setprecision(2) << sum; return 0;
}
直接用上课的作业,就可以啦! 别忘了用#include <iomanip>就好啦。 思路不用说了吧,上过课的都知道。 可供直接拷贝。
-
1
👍 #include<iostream> #include<algorithm> #include <iomanip> using namespace std; int n,t; struct Jinbi { int m,v; double avg; }j[105]; bool cmp(Jinbi x,Jinbi y) { return x.avg>y.avg; } int main() { cin>>n>>t; for(int i=1;i<=n;i++) { cin>>j[i].m>>j[i].v; j[i].avg=j[i].v*1.00/j[i].m; } sort(j+n,j+n+1,cmp); int pos=0; double sum=0; while(t>0) { int ans=min(t,j[pos].m); t-=ans; sum+=ans*j[pos].avg; pos++; } cout<<fixed<<setprecision(2)<<sum; return 0; }
-
1
本题就是作业 直接拷贝过来就行了
#include <iostream> #include <algorithm> using namespace std; struct gold { int num, power; double avg; }; gold a[10005]; bool cmp(gold a, gold b) { return a.avg > b.avg; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i].num >> a[i].power; a[i].avg = 1.0 * a[i].power / a[i].num; } sort(a, a + n, cmp); int pos = 0, temp; double sum = 0; while (m > 0) { temp = min(a[pos].num, m); if (m - temp > 0) { sum += a[pos].power; } else { sum += a[pos].avg * m; } m -= temp; pos++; } printf("%.2lf",sum); return 0; }
-
1
#include <cstdio> #include <algorithm> using namespace std; int n,pos; double t,sum=0,tmp; struct Gold{ double m,v,avg; }a[100]; bool cmp(Gold x,Gold y){ return x.avg>y.avg; } int main(){ scanf("%d%lf",&n,&t); for(int i=0;i<n;i++){ scanf("%lf%lf",&a[i].m,&a[i].v); a[i].avg=a[i].v/a[i].m; } sort(a,a+n,cmp); while(t>0)tmp=min(t,a[pos].m),t-=tmp,sum+=tmp*a[pos++].avg; printf("%.2lf\n",sum); return 0; }
-
1
跟核桃上一样 要“三省吾生”!
#include<bits/stdc++.h> using namespace std; int n,t; struct golden { int w; double v,sv; }q[105]; double sum; bool cmp(golden x,golden y) { return x.v>y.v; } int main() { cin>>n>>t; for(int i=1;i<=n;i++) { cin>>q[i].w>>q[i].sv; q[i].v=q[i].sv/q[i].w; } sort(q+1,q+n+1,cmp); int pos=1; while(t>0) { int y=min(q[pos].w,t); t-=y; sum+=q[pos].v*y; pos++; } cout<<fixed<<setprecision(2)<<sum<<endl; return 0; }
-
0
~~
#include <iostream> #include <algorithm> #include <iomanip> using namespace std; struct Coin { double m,v,avg; } a[10005]; bool cmp(Coin a,Coin b) { return a.avg > b.avg; } int main() { int n, t; cin >> n >> t; for (int i = 0; i < n; i++) { cin >> a[i].m >> a[i].v; a[i].avg = a[i].v/a[i].m; } sort(a, a + n, cmp); int pos = 0; double sum = 0; while (t > 0) { double temp; if(t > a[pos].m) temp = a[pos].m; else temp = t; sum += temp * a[pos].avg,t -= temp; pos++; } cout << fixed << setprecision(2) << sum; return 0; } ```~~
-
0
这道题.怎么说呢,直接就是
作业换了个人名同类型的题好吧......#include <bits/stdc++.h> using namespace std; int n; double T; struct coin { double m,v,avg; }a[105]; bool cmp(coin a,coin b) { return a.avg > b.avg; } int main() { cin >> n >> T; for(int i = 1;i <= n;i++) { cin >> a[i].m >> a[i].v; a[i].avg=a[i].v/a[i].m; } sort(a+1,a+n+1,cmp); double sum = 0; int pos = 1; while(T>0) { double temp=min(a[pos].m,T); T-=temp; sum+=temp*a[pos].avg; pos++; } cout << fixed << setprecision(2) << sum; return 0; }
-
0
P1028 金币 Lv.1
题目描述
阿里巴巴走进了装满宝藏的藏宝洞。藏宝洞里面有 N 堆金币,第 i 堆金币的总重量和总价值分别是 mi,vi。阿里巴巴有一个承重量为 T 的背包,但并不一定有办法将全部的金币都装进去。他想装走尽可能多价值的金币。所有金币都可以随意分割,分割完的金币重量价值比(也就是单位价格)不变。请问阿里巴巴最多可以拿走多少价值的金币?
思路
对标课上作业。将单位价值求出来,并按照单位价值从大到小排序,加入到背包中。
结构体:
struct Coin { double m,v,avg; }a[101];
比较:
bool cmp(Coin a,Coin b) { return a.avg > b.avg; }
参考代码:
#include <iostream>//hetao3097453 #include <algorithm> #include <iomanip> using namespace std; int n; double t; struct Coin { double m,v,avg; }a[101]; bool cmp(Coin a,Coin b) { return a.avg > b.avg; } int main() { cin >> n >> t; for(int i = 0;i < n;i++) { cin >> a[i].m >> a[i].v; a[i].avg = a[i].v / a[i].m; } sort(a,a + n,cmp); int pos = 0; double sum = 0.0; while(t > 0) { double temp = min(a[pos].m,t); sum += a[pos].avg * temp; t -= temp; pos++; } cout << fixed << setprecision(2) << sum << endl; return 0; }
hetao3097453
2023年5月13日
-
-1
哪错了?
#include<iostream> #include<algorithm> #include <iomanip> using namespace std; int n,t; struct Jinbi { int m,v; double avg; }j[105]; bool cmp(Jinbi x,Jinbi y) { return x.avg>y.avg; } int main() { cin>>n>>t; for(int i=1;i<=n;i++) { cin>>j[i].m>>j[i].v; j[i].avg=j[i].v1.00/j[i].m; } sort(j+n,j+n+1,cmp); int pos=0; double sum=0; while(t>0) { int ans=min(t,j[pos].m); t-=ans; sum+=ansj[pos].avg; pos++; } cout<<fixed<<setprecision(2)<<sum; return 0; }
- 1
信息
- ID
- 69
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 2264
- 已通过
- 912
- 上传者