Packets

Time Limit: 1000MS Memory Limit: 10000K

Description

A factory produces products packed in square packets of the same height h and of the sizes 11, 22, 33, 44, 55, 66. 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 11 to the biggest size 66. 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

思路很简单,就是实现起来比较容易乱,画个图就可以知道每个几 * 几对应的应该需要多少个1 * 1或者2 * 2…尽量要都用的话,最好不要留有空隙(本代码是模拟的方法,还有一种数学的方法,在百度上可以搜的到)

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
	int t1, t2, t3, t4, t5, t6;
	while (cin >> t1 >> t2 >> t3 >> t4 >> t5 >> t6 && (t1 + t2 + t3 + t4 + t5 + t6) != 0) {
		int sum = 0;
		sum += t6;
		sum += t5;
		t1 = max(0, t1 - 11 * t5);
		sum += t4;
		if (t2 < 5 * t4) t1 = max(0, t1 - (5 * t4 - t2));
		t2 = max(0, t2 - 5 * t4);
		sum += (t3 + 3) / 4;
		t3 %= 4;
		if (t3 == 1) {
			if (t2 < 5) t1 = max(0, t1 - (27 - 4 * t2));
			else t1 = max(0, t1 - 7);
			t2 = max(0, t2 - 5);
		} else if (t3 == 2) {
			if (t2 < 3) t1 = max(0, t1 - (18 - 4 * t2));
			else t1 = max(0, t1 - 6);
			t2 = max(0, t2 - 3);
		} else if (t3 == 3) {
			if (t2 < 1) t1 = max(0, t1 - (9 - 4 * t2));
			else t1 = max(0, t1 - 5);
			t2 = max(0, t2 - 1);
		}
		sum += (t2 + 8) / 9;
		t2 %= 9;
		if (t2) t1 = max(0, t1 - (36 - t2 * 4));
		sum += (t1 + 35) / 36;
		cout << sum << endl;
	}
	return 0;
}