题目大意:有货物若干,大小分别为1*1,2*2,3*3,4*4,5*5,6*6,将货物装在6*6大小的箱子中,求最少的箱子个数。
题目链接:http://poj.org/problem?id=1017
简单贪心,从大货物开始考虑即可。
#include <stdio.h>
#include <math.h>
#include <string.h>
int x[7];
bool end;
int main()
{
while (1)
{
end = true;
for (int i = 1; i <= 6; i++)
{
scanf("%d",&x[i]);
if (x[i] != 0)
{
end = false;
}
}
if (end)
break;
int pNum=0;
int tNum = 0;
pNum = x[6];
x[6] = 0;
tNum += pNum;
pNum = x[5];
x[5] = 0;
if (pNum * 11 > x[1])
x[1] = 0;
else
x[1] = x[1] - pNum * 11;
tNum += pNum;
pNum = x[4];
x[4] = 0;
if (pNum * 5 > x[2])
{
if ((pNum * 5 - x[2]) * 4 > x[1])
x[1] = 0;
else
x[1] = x[1] - (pNum * 5 - x[2]) * 4;
x[2] = 0;
}
else
{
x[2] = x[2] - pNum * 5;
}
tNum += pNum;
pNum = x[3] / 4;
if (x[3] % 4 != 0)
{
pNum++;
if ((4 - x[3] % 4) * 2 - 1 > x[2])
{
if (3 * 3 * (4 - x[3] % 4) - 2 * 2 * x[2] > x[1])
x[1] = 0;
else
x[1] = x[1] - (3 * 3 * (4 - x[3] % 4) - 2 * 2 * x[2]);
x[2] = 0;
}
else
{
if (3 * 3 * (4 - x[3] % 4) - 2 * 2 * ((4 - x[3] % 4) * 2 - 1) > x[1])
x[1] = 0;
else
x[1] = x[1] - (3 * 3 * (4 - x[3] % 4) - 2 * 2 * ((4 - x[3] % 4) * 2 - 1));
x[2] = x[2] - ((4 - x[3] % 4) * 2 - 1);
}
}
x[3] = 0;
tNum += pNum;
pNum = x[2] / 9;
if (x[2] % 9 != 0)
{
pNum++;
if ((9 - x[2] % 9) * 4 > x[1])
x[1] = 0;
else
x[1] = x[1] - (9 - x[2] % 9) * 4;
}
x[2] = 0;
tNum += pNum;
pNum = x[1] / 36;
if (x[1] % 36 != 0)
{
pNum++;
}
x[1] = 0;
tNum += pNum;
printf("%d\n",tNum);
}
return 0;
}