POJ 1017 Packets
Description
A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6*6. Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.
Input
The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size 1*1 to the biggest size 6*6. The end of the input file is indicated by the line containing six zeros.
Output
The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last ``null'' line of the input file.
Sample Input
0 0 4 0 0 1 7 5 1 0 0 0 0 0 0 0 0 0
Sample Output
2 1
Source
题目大致意思:就是有几种东西需要包装,分别是1*1 2*2 3*3 4*4 5*5 6*6的尺寸,至于高度不用考虑,因为保证高度和物品的高度是一样的。现在用6*6的包装装上述的东西,问最少需要几个包装。
分析:
1.6*6的单独占一个包装而且还没有剩余空间
2.5*5的单独占一个包装,有11个1*1的剩余空间
3.4*4的单独占一个包装,有20个1*1的剩余空间或5个2*2的剩余空间
4.3*3的比较特殊,因为四个3*3的刚好占满一个包装,所以3*3的占的包装应该是(pack3+4)/4
5.剩下的1*1 2*2的先从上面的剩余空间里面去补,不够在增加6*6的包装就好
很简单的一道题,主要是能够分析清楚不同大小的包装占空间的关系就很容易得出答案
代码如下:
#include <iostream> using namespace std; int pack1, pack2, pack3, pack4, pack5, pack6; int packet3[] = { 0,5,3,1 }; //3*3剩余2*2空间的数量 int main() { int bag; int box1, box2; while (cin >> pack1 >> pack2 >> pack3 >> pack4 >> pack5 >> pack6 && (pack1 + pack2 + pack3 + pack4 + pack5 + pack6)) { bag = pack6 + pack5 + pack4 + (pack3 + 3) / 4; box2 = pack4 * 5 + packet3[pack3 % 4]; if (pack2 > box2) bag += (pack2 - box2 + 8) / 9; box1 = bag * 36 - pack2 * 4 - pack3 * 9 - pack4 * 16 - pack5 * 25 - pack6 * 36; if (pack1 > box1) bag += (pack1 - box1 + 35) / 36; cout << bag << endl; } }