1. 程式人生 > >POJ 1017 Packets

POJ 1017 Packets

represent 應該 arc 簡單的 res 分析 for each rod it is

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

Central Europe 1996 題目大致意思:就是有幾種東西需要包裝,分別是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;
    }
}

POJ 1017 Packets