#include "stdafx.h"
const double goods[] = {0.5, 0.2, 0.33, 0.28, 0.88, 0.01, 0.02, 0.05, 0.77, 0.62};
const double maxLoad = 1.0;
int numBoxes = sizeof(goods) / sizeof (goods[0]);
int nMinBoxes = numBoxes;
unsigned int bitmap = (1 << numBoxes) - 1;
void Packaging(int nBoxes, double curLoad, unsigned int used)
{
if ((used & bitmap) == bitmap)
{
if (nBoxes < nMinBoxes)
{
nMinBoxes = nBoxes;
}
return;
}
for (int i = 0; i < numBoxes; ++i)
{
double load = curLoad;
unsigned int 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 main(void)
{
Packaging(0, 1.0, 0);
_tprintf(_T("At least %d boxes %s needed\n"), nMinBoxes, nMinBoxes > 1 ? _T("are") : _T("is"));
getchar();
return 0;
}
临时文章
最新推荐文章于 2022-09-02 17:33:18 发布