一般背包问题(非0/1),找错

la_feng 2008-11-12 12:52:40
我调了半天看不出来,拿出来给大家看看吧,群众的眼睛是雪亮的。
//BackPack.h

class Object
{
public:
float value;
float weight;
};
#include<iostream>
#include"BackPack.h"
using namespace std;

void main()
{
float total=120;
float TotalValue=0;
Object obj[10]={50,17,60,30,70,25,80,41,90,80,80,70,70,64,60,56,50,47,40,38};
Object TempObj;
float xi[10]={0,0,0,0,0,0,0,0,0,0};
float price[10];
float temp[10];
float xch;
for(int i=0;i<10;i++)
{
price[i]=(obj[i].value)/(obj[i].weight);
temp[i]=price[i];
}
for(i=0;i<10;i++)
for(int j=i+1;j<10;j++)
{
if(temp[i]<temp[j])
{
xch=temp[i];
temp[i]=temp[j];
temp[j]=xch;

}
}

for(i=0;i<10;i++)
for(int j=0;j<10;j++)
{
if(price[j]==temp[i])
{
if(total>obj[j].weight)
{
xi[j]=1;
total=total-obj[j].weight;
TotalValue+=obj[j].value;
}
else
{
xi[j]=total/obj[j].weight;
TotalValue+=xi[j]*obj[j].value;
}
}
}
for(i=0;i<10;i++)
cout<<xi[i]<<endl;
cout<<TotalValue<<endl;
}
...全文
88 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
la_feng 2008-11-12
  • 打赏
  • 举报
回复
背包问题描述如下: 已知
背包容量M=120
物品种类数n=10
各种物品的总效益pi(i=1,2,………10) : 50,60,70,80,90,80,70,60,50,40
各种物品的总重量wi(i=1,2………10) : 17,30,25,41,80,70,64,56,47,38
求: 各种物品所取重量占其总重量的比例xi(i=1,2,…..10),满足0<=xi<=1,
la_feng 2008-11-12
  • 打赏
  • 举报
回复
附上正确答案
1 1 1 1 0 0.1 0 0 0 0 268.00
不明白的地方是为什么正确答案为0的地方我的程序出来会有值
la_feng 2008-11-12
  • 打赏
  • 举报
回复
已自己解决,结贴

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧