文章
166
粉丝
68
获赞
831
访问
51.9k
- #include <bits/stdc++.h>
- using namespace std;
-
- struct node{
- float mi;
- float wi;
- float good;
- node(float a,float b,float c){
- mi=a;
- wi=b;
- good=c;
- };
- };
-
- bool rule(node a,node b){
- return a.good<b.good;
- };
-
- int main(){
- float x;
- int n;
- while(cin>>x>>n){
- if(x<0||n<0)break;
- float mi,wi,good;
- vector<node> goods;
- while(n--){
- cin>>mi>>wi;
- good=wi/mi;
- node nd{mi,wi,good};
- goods.push_back(nd);
- }
- sort(goods.begin(),goods.end(),rule);
- float ans=0;
- for(int i=0;i<goods.size();i++){
- if(x>=goods[i].wi){
- x-=goods[i].wi;
- ans+=goods[i].mi;
- }else{
- //if的写法为什么过不完,我知道了,大于0的也是最后的一步了,应该break
- if(x>0){
- ans+=(x/goods[i].good);
- break;
- }else{
- break;
- }
- }
- }
- printf("%.3f\n",ans);
- }
- return 0;
- }
登录后发布评论
暂无评论,来抢沙发