題意:1x1 到 6x6 的行李,要裝在 6x6 的箱子裡,求最小的箱子數。
方法:貪心。
2x2為負用1x1補正很好,希望大家借鑒。
AC代碼:
#define Local #include#include #include #include #include #include #include #include #include #include using namespace std; int p[7], sum; void judge_1() { if (p[2] < 0) { p[1] += p[2] * 4; p[2] = 0; } } void judge_2(int a, int b) { p[2] -= a; p[1] -= b; judge_1(); } int main() { #ifdef Local freopen(a.txt, r, stdin); #endif while (true) { int flag = 0; sum = 0; for (int i = 1; i <= 6; i++) { cin >> p[i]; flag += p[i]; } if (!flag) break; sum += p[6]; if (p[5]) { sum += p[5]; p[1] -= p[5] * 11; } if (p[4]) { sum += p[4]; p[2] -= p[4] * 5; judge_1(); } if (p[3]) { int mod = p[3] % 4; sum += p[3] / 4; if (mod) { ++sum; if (1 == mod) judge_2(5, 7); else if (2 == mod) judge_2(3, 6); else if (3 == mod) judge_2(1, 5); } } if (p[2] > 0) { int mod = p[2] % 9; sum += p[2] / 9; if (mod) { ++sum; p[1] -= 36 - mod * 4; } } if (p[1] > 0) { int mod = p[1] % 36; sum += p[1] / 36; if (mod) ++sum; } cout << sum << endl; } return 0; }