题意:有1*1到6*6的的东西,需要用6*6的箱子将它们装起来。问:至少需要多少个6*6箱子
思路:
- 一个瓶子怎么装东西最多?先装石头,在装沙子,然后装水。
- 同样放在本题就是先装6*6然后5*5.....
想法上的一些解释:(如何装呢)
- 6*6物品n件,需要箱子n个
- 5*5物品n件,需要箱子n个,剩下 36n-25n个1*1的空余
- 4*4物品n件,需要箱子n个,剩下5n个2*2的空余,1*1也有也有空余
- 3*3物品n件,需要箱子(n+3)/4个(向上取余),如果只装1个3*3的箱子剩下5个2*2空余,2个3*3的箱子剩下3个2*2空余,3个3*3的箱子剩下1个2*2空余
- ......
装箱子过程中的代码实现:
ans = p[6] + p[5] + p[4] + (p[3] + 3) / 4; int cn_2 = p[4] * 5 + dir[p[3] % 4]; if (cn_2 < p[2]) { ans += (p[2] - cn_2 + 8) / 9; } int cn_1 = ans * 36 - p[6] * 36 - p[5] * 25 - p[4] * 16 - p[3] * 9 - p[2] * 4; if (cn_1 < p[1]) { ans += (p[1] - cn_1 + 35) / 36; }
解题的代码:
#include <iostream> #include <cstdio> using namespace std; int dir[4] = { 0,5,3,1 }; int p[6]; int main() { int sum, ans; while (true) { sum = 0; for (int i = 1; i <= 6; i++) { scanf("%d", &p[i]); sum += p[i]; } if (!sum) { break; } ans = p[6] + p[5] + p[4] + (p[3] + 3) / 4; int cn_2 = p[4] * 5 + dir[p[3] % 4]; if (cn_2 < p[2]) { ans += (p[2] - cn_2 + 8) / 9; } int cn_1 = ans * 36 - p[6] * 36 - p[5] * 25 - p[4] * 16 - p[3] * 9 - p[2] * 4; if (cn_1 < p[1]) { ans += (p[1] - cn_1 + 35) / 36; } printf("%d\n", ans); } return 0; }