您的位置:首页 > 其它

临时文章

2010-02-05 01:20 113 查看
#include "stdafx.h"

double goods[] = {0.5, 0.2, 0.33, 0.28, 0.88, 0.01, 0.02, 0.05, 0.77, 0.62};
int num = sizeof(goods) / sizeof (goods[0]);
int nMinBoxes = num;
const double maxLoad = 1.0;

void Packaging(int nBoxes, double curLoad, unsigned short used)
{
if ((used & 0x03FF) == 0x03FF)
{
if (nBoxes < nMinBoxes)
{
nMinBoxes = nBoxes;
}
return;
}

for (int i = 0; i < num; ++i)
{
double load = curLoad;
unsigned short mask = used;
if (((mask >> i) & 1) == 0)
{
mask |= (1 << i);
if (goods[i] + load > maxLoad)
{
++nBoxes;
load = goods[i];
}
else load += goods[i];
Packaging(nBoxes, load, mask);
}
}
}

int _tmain(int argc, _TCHAR* argv[])
{
Packaging(0, 1.0, 0);
_tprintf(_T("At least %d boxes %s needed/n"), nMinBoxes, nMinBoxes > 1 ? _T("are") : _T("is"));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: