话不多说,直接上代码:
//miss 标注的是我提交Wrong Answer 之后发现的错误
package POJ; //NOTE: delete this line when you submit your answer!
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) {
int [] a = new int[6];
a[0] = in.nextInt();
a[1] = in.nextInt();
a[2] = in.nextInt();
a[3] = in.nextInt();
a[4] = in.nextInt();
a[5] = in.nextInt();
int sum = a[0] + a[1] + a[2] + a[3] + a[4] + a[5];
if (sum == 0) {
break;
}
int result = calculate(a);
System.out.println(result);
}
}
public static int calculate(int [] a) {
int p_1 = a[0]; int p_2 = a[1]; int p_3 = a[2];
int p_4 = a[3]; int p_5 = a[4]; int p_6 = a[5];
int sum = 0;
sum = sum + p_6;
//cal_5
if (p_5 > 0) {
sum = sum + p_5;
//cal_1 to make up
if (p_1 >= (p_5 * 11)) {
p_1 = p_1 - (p_5 * 11);
}
else {p_1 = 0;}
}
//cal_4
if (p_4 > 0) {
sum = sum + p_4;
//cal_2 to make up
if (p_2 >= (p_4 * 5)) {
p_2 = p_2 - (p_4 * 5);
}
else {
int r_num = (20 * p_4) - (4 * p_2);
p_2 = 0; //miss
//cal_1 to make up
if (p_1 >= r_num) {
p_1 = p_1 - r_num;
}
else {p_1 = 0;}
}
}
//cal_3
if (p_3 > 0) {
sum = sum + (p_3 / 4);
if (p_3 % 4 == 1) {
sum++;
//cal_2 to make up
if (p_2 >= 5) {
p_2 = p_2 - 5;
//cal_1 to make up
if (p_1 >= 7) {
p_1 = p_1 - 7;
}
else {
p_1 = 0;
}
}
else {
int r_num = 27 - (4 * p_2);
p_2 = 0; //miss
//cal_1 to make up
if (p_1 >= r_num) {
p_1 = p_1 - r_num;
}
else {
p_1 = 0;
}
}
}
else if (p_3 % 4 == 2) {
sum++;
//cal_2 to make up
if (p_2 >= 3) {
p_2 = p_2 - 3;
//cal_1 to make up
if (p_1 >= 6) {
p_1 = p_1 -6;
}
else {
p_1 = 0;
}
}
else {
int r_num = 18 - (4 * p_2);
p_2 = 0; // miss
//cal_1 to make up
if (p_1 >= r_num) {
p_1 = p_1 - r_num;
}
else {
p_1 = 0;
}
}
}
else if (p_3 % 4 == 3) {
sum++;
//cal_2 to make up
if (p_2 >= 1) {
p_2 = p_2 - 1;
//cal_1 to make up
if (p_1 >= 5) {
p_1 = p_1 -5;
}
else {
p_1 = 0;
}
}
else {
p_2 = 0;
//cal_1 to make up
if (p_1 >= 9) {
p_1 = p_1 - 9;
}
else {p_1 = 0;}
}
}
}
//cal_2
if (p_2 > 0) {
sum = sum + (p_2 / 9);
if (p_2 % 9 != 0) { //miss
sum++;
int r_num = 36 - (4 * (p_2 % 9));
//cal_1 to make up
if (p_1 >= r_num) {
p_1 = p_1 - r_num;
}
else {p_1 = 0;}
}
}
//cal_1
if (p_1 > 0) {
sum = sum + (p_1 / 36);
if (p_1 % 36 != 0) {
sum++;
}
}
return sum;
}
}
解题思路:
装箱肯定是要从大盒子开始装,即从6*6的箱子开始到1*1的箱子结束。原则就是,用尽量大的箱子来填满空间(其实本题只涉及到用2*2或1*1的箱子来填充剩余空间)。
6*6箱子:本身占一个单位;
5*5箱子:本身占一个单位,其余用11个1*1的箱子填充;
4*4箱子:本身占一个单位,其余用5个2*2的箱子填充;
3*3箱子:4个占一个单位。故要分四种情况(设其数量为n)考虑:
若(n%4 == 0): 理想情况,没什么要担心的;
若(n%4 == 1): 要用5个2*2和7个1*1的箱子填充;
若(n%4 == 2): 要用3个2*2和6个1*1的箱子填充;
若(n%4 == 3): 要用1个2*2和5个1*1的箱子填充;
2*2箱子:9个占一个单位。其余用1*1的箱子填充;
1*1箱子:36个占一个单位。若无法整除,需要额外的一个箱子。
当然,上面的都是理想情况;即,如果用来填充的2*2的箱子数不够,则需要1*1的箱子来进行替代填充(1*1的箱子也存在用完的情况,这时以后的装箱就不再考虑填充的情况了)。