社区
非技术区
帖子详情
C的装箱问题,谁有思路!
doer_ljy
2003-03-12 09:31:30
C的装箱问题,谁有思路!
...全文
202
13
打赏
收藏
C的装箱问题,谁有思路!
C的装箱问题,谁有思路!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
13 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
doer_ljy
2003-03-12
打赏
举报
回复
kingofvc(现实就是不公)兄,这的份不够分的!
令开一个贴子,单独给分!
呵呵!
http://expert.csdn.net/Expert/topic/1519/1519843.xml?temp=.3213617
cicirain
2003-03-12
打赏
举报
回复
only 蹭分~~ :P
kingofvc
2003-03-12
打赏
举报
回复
更正 “你可以把堆栈改成对了 ” 应为你可以把堆栈改成队列
kingofvc
2003-03-12
打赏
举报
回复
另外一点 我使用了goto 主要是偷懒 希望你不要这么做:)
后来我用同样的算法帮同学写了一个c的 不过我这里已经没有原程序了 (没用goto)
kingofvc
2003-03-12
打赏
举报
回复
背包问题啊 运筹学分支定界法 不过由于当时误解了算法 使用了堆栈 影响了程序的效率
你可以把堆栈改成对了 然后把堆栈的弹出改为队列中取最大值即可
这里主要是解决第一种问题 对于第二种 其实也在程序的过程中
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <stack>
#include <float.h>
using namespace std;
class CNode {//存放节点信息 主要用于堆栈
public:
CNode(int number){
value=0;
pos=0;
pGood=new int[number];
memset(pGood,0,number*sizeof(int));
}
~CNode(){
if(pGood!=NULL)
delete []pGood;
}
double value;
int pos;
int *pGood;
};
class CGood{//存放物品信息
public :
CGood(int number){
num=number;
average=new double[number];
pWeight=new double[number];
pValue=new double[number];
no=new int[number];
}
~CGood(){
if(pWeight!=NULL)
delete []pWeight;
if(pValue!=NULL)
delete []pValue;
if(average!=NULL)
delete []average;
if(no!=NULL)
delete []no;
}
void GetData(){
for(int i=0;i<num;i++){
cout<<"请输入物品"<<i+1<<"的重量:"<<endl;
cin>>pWeight[i];
cout<<"请输入物品"<<i+1<<"的价值:"<<endl;
cin>>pValue[i];
average[i]=pValue[i]/pWeight[i];
no[i]=i;
}
}
void ShowAll(){
for(int i=0;i<num;i++){
cout<<"物品"<<no[i]+1<<" 重"<<pWeight[i]<<" 价值"<<pValue[i]<<" 平均价值"<<average[i]<<endl;
}
}
void Sort(){
for(int i=0;i<num;i++){
double min=average[0];
int pos=0;
for(int j=0;j<num-i;j++){
if(min>average[j]){
min=average[j];
pos=j;
}
}
double temw=pWeight[num-i-1];
double temv=pValue[num-i-1];
double tema=average[num-i-1];
int temn=no[num-i-1];
pWeight[num-i-1]=pWeight[pos];
pValue[num-i-1]=pValue[pos];
average[num-i-1]=average[pos];
no[num-i-1]=no[pos];
pWeight[pos]=temw;
pValue[pos]=temv;
average[pos]=tema;
no[pos]=temn;
}
}
double GetWeight(){
double all=0;
for(int i=0;i<num;i++)
all+=pWeight[i];
return all;
}
double GetValue(){
double all=0;
for(int i=0;i<num;i++)
all+=pValue[i];
return all;
}
double GetMinWeight(){
double min=pWeight[0];
for(int i=1;i<num;i++)
if(min>pWeight[i])
min=pWeight[i];
return min;
}
int num;
double *average;
double *pWeight;
double *pValue;
int *no;
};
stack <double> mystack;
stack <int> datastack;
void main(){
CNode *one,*another;
double weight;
cout<<"请输入总共允许的货物总量:"<<endl;
cin>>weight;
int number;
cout<<"请输入物品个数:"<<endl;
cin>>number;
one=new CNode(number);
another=new CNode(number);
CNode max(number);
CGood all(number);
all.GetData();
while(all.GetWeight()<weight){
cerr<<"物品总共重量小于允许所带物品的总量!无意义"<<endl<<"请重新输入:"<<endl;
all.GetData();
}
while(all.GetMinWeight()>weight){
cerr<<"物品最小重量大于于允许所带物品的总量!无意义"<<endl<<"请重新输入:"<<endl;
all.GetData();
}
all.ShowAll();
all.Sort();
cout<<"排序以后:"<<endl;
all.ShowAll();
loop:
do {
int pos=one->pos;
one->pGood[pos]=1;
another->pGood[pos]=-1;
double oneweight=0;
double anotherweight=0;
int i;
double readlyone=0;
double readlyanother=0;
one->value=0;
another->value=0;
for(i=0;i<=pos;i++){
if(one->pGood[i]==1){
oneweight+=all.pWeight[i];
one->value+=all.pValue[i];
}
if(another->pGood[i]==1){
anotherweight+=all.pWeight[i];
another->value+=all.pValue[i];
}
}
readlyone=oneweight;
readlyanother=anotherweight;
int j=i;
while(oneweight<=weight&&i<number){
oneweight+=all.pWeight[i];
one->value+=all.pValue[i];
i++;
}
while(anotherweight<=weight&&j<number){
anotherweight+=all.pWeight[j];
another->value+=all.pValue[j];
j++;
}
i--;
j--;
if(!(i==number-1&&j==number-1)){
one->value-=(all.average[i]*(oneweight-weight));
another->value-=(all.average[j]*(anotherweight-weight));
}
if(readlyone>weight)
one->value=-DBL_MAX;
if(readlyanother>weight)
another->value=-DBL_MAX;
cout<<"one value:"<<one->value<<endl<<"another value:"<<another->value<<endl<<endl;
if(one->value>=another->value){
if(another->value!=-DBL_MAX){
cout<<"push another "<<another->value<<endl;
mystack.push(another->value);
datastack.push(another->pos);
for(int j=0;j<number;j++)
datastack.push(another->pGood[number-j-1]);
}
for(int i=0;i<number;i++)
another->pGood[i]=one->pGood[i];
another->pos=one->pos;
}
else{
if(one->value!=-DBL_MAX){
cout<<"push one "<<one->value<<endl;
mystack.push(one->value);
datastack.push(one->pos);
for(int j=0;j<number;j++)
datastack.push(one->pGood[number-j-1]);
}
for(int i=0;i<number;i++)
one->pGood[i]=another->pGood[i];
one->pos=another->pos;
}
one->pos++;
another->pos=one->pos;
if(pos==number-1){
CNode *p=one->value>another->value? one:another;
max.value=p->value;
double maxweight=0;
for(i=0;i<number;i++){
max.pGood[i]=p->pGood[i];
if(p->pGood[i]==1)
maxweight+=all.pWeight[i];
}
if(maxweight>weight)
max.value=-DBL_MAX;
}
} while(one->pGood[number-1]==0);
while(!mystack.empty()){
one->value=mystack.top();
for(int i=0;i<number;i++){
one->pGood[i]=datastack.top();
datastack.pop();
}
one->pos=datastack.top()+1;
datastack.pop();
mystack.pop();
if(max.value<one->value){
mystack.push(max.value);
datastack.push(max.pos);
for(int j=0;j<number;j++)
datastack.push(max.pGood[number-j-1]);
for(int i=0;i<number;i++){
another->pGood[i]=one->pGood[i];
another->pos=one->pos;
}
goto loop;
}
}
cout<<"value:"<<max.value<<endl<<"选择的物品是"<<endl;
for(int j=0;j<number;j++)
if(max.pGood[j]==1)
cout<<all.no[j]+1<<"\t";
cout<<endl;
system("pause");
}
doer_ljy
2003-03-12
打赏
举报
回复
就是假设你现在有1件、2件、5件、10件四种包裹各n个,现在有一个N个容量的箱子
1、不拆包,尽量先装大包裹,那么最佳的装箱方案是?
2、如果拆包,尽量不拆包,装满箱子的最佳方案。
alextao
2003-03-12
打赏
举报
回复
你的问题和没问一样啊!
vanmvanm
2003-03-12
打赏
举报
回复
leader啊,你把你的问题再描述描述!肯定会有能人给你回复的,偶就算了吧^_^
vanmvanm
2003-03-12
打赏
举报
回复
呵呵,偶是西红柿,leader,不是吧,你的帖子这么受欢迎啊,^_^,偶们来帮你顶~~~~~~
kicool
2003-03-12
打赏
举报
回复
??
ninesong
2003-03-12
打赏
举报
回复
描述一下问题吧。
kgww
2003-03-12
打赏
举报
回复
我是来接分得!
alex_deng
2003-03-12
打赏
举报
回复
用我的名字mark一下
掌握集装箱(Container)思潮与Docker技术
随着近年来Docker集装箱技术的流行,这种集装箱式架构设计,可说红透半天边,而且势如破竹、蔚为风潮,正在强力改变终端与云平台的整体设计理念和方法。这跟半世纪前,海上船运业的集装箱风潮,对整个产业的革命性...
c语言
装箱问题
思路
,C语言基于贪心算法解决
装箱问题
的方法
本文实例讲述了C语言基于贪心算法解决
装箱问题
的方法。分享给大家供大家参考,具体如下:问题描述:有一些箱子,容量为V,同时有n个物品,每个物品有一个体积(小于等于箱子容量),要求将物品全部装入箱子中,使占用...
装箱问题
c语言函数,C语言基于贪心算法解决
装箱问题
的方法
本文实例讲述了C语言基于贪心算法解决
装箱问题
的方法。分享给大家供大家参考,具体如下:问题描述:有一些箱子,容量为V,同时有n个物品,每个物品有一个体积(小于等于箱子容量),要求将物品全部装入箱子中,使占用...
装箱问题
C语言
假设有N项物品,大小分别为s1 、s2 、…、si 、…、sN ,其中si 为满足1≤si ≤100的整数。要把这些物品装入到容量为100的一批箱子(序号1-N)中。装箱方法是:对每项物品, ...
装箱问题
POJ1017
简单贪心,模拟。
装箱问题
的详细分析过程和代码。
非技术区
15,440
社区成员
58,165
社区内容
发帖
与我相关
我的任务
非技术区
C/C++ 非技术区
复制链接
扫一扫
分享
社区描述
C/C++ 非技术区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章