那位大哥知道c语言背包问题设计思路啊 跪求!!!

来源:百度知道 编辑:UC知道 时间:2024/05/12 05:02:51

#include<iostream.h>
#include<stdlib.h>

struct goods
{char N[2];
float W;
float P;
} g[5]={{"a",1.5,1.2},{"b",3,3},{"c",5,8},{"d",4,7},{"e",2.5,5}},temp,a;
int i,j,z;
float c=8,b=0;

void px()
{for(i=0;i<5;i++)
{for(j=i+1;j<5;j++)
if(g[i].W>g[j].W)
{temp=g[i];
g[i]=g[j];
g[j]=temp;
}
}
}

void xx()
{cout<<"你可免费获得的货物体积最大为8\n";
cout<<"你可选商品信息:"<<endl;
cout<<"商品名 商品体积 商品单价\n";
for(i=0;i<5;i++)
cout<<g[i].N<<" "<<g[i].W<<" "<<g[i].P<<endl;
}

void qu()
{for(i=0;i<5&&c>=0;i++)
{c=c-g[i].W;
b=b+g[i].P;}
c=c+g[i-1].W+g[i-2].W;
b=b-g[i-1].P-g[i-2].W;