题意
题目的大意是,有若干个边长为1,2,3,4,5,6的正方形物品,需要放在边长为6的正方形盒子里,物品和盒子的高度均相同,问最少需要多少个盒子把所有物品装下。
分析
先考虑边长为4,5,6的,显然这每个物品都需要一个盒子,另外边长为3的每四个需要一个盒子。边长为4的物品装下后还可以拿来装边长为2的,边长3的装下后若有剩余空间也可以拿来装边长为2的,这样最后再考虑边长为1的,把先前的盒子填满后,若还有边长为1或2的再用新盒子装。
代码如下:
Memory: 248K Time: 32MS Length:23LINES
#include<iostream>
using namespace std;
int box[] = {0, 5, 3, 1};
inline bool input(int* s)
{
int i = 0;
while(i < 6 && cin >> s[i++]);
return s[0] + s[1] + s[2] + s[3] + s[4] + s[5] > 0;
}
int main()
{
int size[6];
while(input(size))
{
int count = size[5] + size[4] + size[3] + (size[2] + 3) / 4;
int len2 = 5 * size[3] + box[size[2] % 4]; //装边长2的
if(size[1] > len2) count += (size[1] - len2 + 8) / 9; //若还有边长为2的
int len1 = 36*count - 36*size[5] - 25*size[4] - 16*size[3] - 9*size[2] - 4*size[1];//装边长1的
if(size[0] > len1) count += (size[0] - len1 + 35) / 36; //还有边长1的
cout << count << endl;
}
return 0;
}