地址 http://poj.org/problem?id=1017
题目大意是 有个6*6的面积的集装箱,装上1*1 2*2 3*3 4*4 5*5 6*5的箱子,询问最少需要集装箱的数量 多行输入 每行 6个数字以空格隔开 表示1~6面积的箱子的数目 6个数字全0 表示输入结束 按照输入的行数 输出多行答案 每行为最少需要集装箱的数量
解答 贪心解法 先填入最大的箱子,剩余的空间填入小箱子
可知 6*6可填充一个集装箱 无缝隙可填充其余箱子
5*5可填充一个集装箱 剩余缝隙可以填充6*6-5*5 =11个1*1的箱子
4*4可填充一个集装箱 剩余缝隙可填充5个2*2的箱子 其余可填充1*1的箱子
3*3比较麻烦 4个3*3可以装满一个集装箱
1~3个3*3的箱子填充如下 配图 填充1到3个3*3的箱子后 ,可以填充2*2的箱子的个数是5 ,3,1
按照这种情况模拟的代码
// 1123555.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <algorithm> using namespace std; /* Sample Input 99 0 0 0 0 0 0 99 0 0 0 0 0 0 99 0 0 0 0 0 0 99 0 0 0 0 0 0 99 0 0 0 0 0 0 99 99 0 99 0 99 0 99 99 2 0 99 0 0 2 0 99 0 0 0 3 7 99 0 0 9 3 7 99 0 0 9 9 9 99 0 0 9 9 9 0 99 0 99 99 9 0 99 0 99 99 9 0 0 0 0 0 0 0 0 0 Sample Output 2 1 */ int arr[6]; int ans; void fill11(int left) { int fillcount = min(arr[0], left); arr[0] -= fillcount; left -= fillcount; } void solve() { ans = 0; ans += arr[5]; //6*6 if (arr[4] != 0) { int count = arr[4];arr[4] = 0; for (int i = 0; i < count; i++) { ans++;fill11(6 * 6 - 5 * 5); } } if (arr[3] != 0) { int count = arr[3];arr[3] = 0; for (int i = 0; i < count; i++) { ans++; int fill22 = min(arr[1], 5); arr[1] -= fill22; fill11(6 * 6 - 4 * 4 - fill22*2*2); } } if (arr[2] != 0) { int count = arr[2]; arr[2] = 0; ans += count / 4; count = count % 4; if (count != 0) { ans++; int fill22 = min(arr[1], (3- count)*2+1); arr[1] -= fill22; fill11(6 * 6 - 3 * 3*count - fill22 * 2 * 2); } } if (arr[1] != 0) { int count = arr[1]; arr[1] = 0; ans += count / 9; count = count % 9; if (count != 0) { ans++; fill11(6 * 6 - count * 4); } } if (arr[0] != 0) { ans += arr[0] / 36; arr[0] = arr[0] % 36; if(arr[0] != 0){ ans++; fill11(6 * 6); } } } int main() { while (1) { ans = 0; int isAllZero = 1; for (int i = 0; i <= 5; i++) { cin >> arr[i]; if (arr[i] != 0) isAllZero = 0; } if (1 == isAllZero) break; solve(); cout << ans << endl; } return 0; }