临时文章

来源:互联网 发布:手机淘宝如何支付 编辑:程序博客网 时间:2024/05/08 21:34

#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;
}

原创粉丝点击