Description
A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. 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 1*1 to the biggest size 6*6. 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
题意:一个工厂制造的产品形状都是长方体盒子,它们的高度都是 h,长和宽都相等,一共有六个型号,分别为1*1, 2*2, 3*3, 4*4, 5*5, 6*6。
这些产品通常使用一个 6*6*h 的长方体箱子包装然后邮寄给客户。因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的箱子数量BoxNum。
思路:6*6的盒子,每个盒子独占一个箱子。
5*5的盒子,每个盒子放入一个箱子,该箱子的剩余空间允许放入的最大尺寸为1*1,且最多放11个。
4*4的盒子,每个盒子放入一个箱子,该箱子的剩余空间允许放入的最大尺寸为2*2。
3*3的盒子,每4个刚好独占一个箱子,不足4个3*3的,剩下空间由2*2和1*2填充。
2*2的盒子和1*1的盒子主要用于填充其他箱子的剩余空间,填充后的多余部分才开辟新箱子装填。
import java.util.Scanner;
public class Main{
private static int box1 ,box2,box3,box4,box5,box6,boxNum=0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
boxNum = 0;
box1 = scanner.nextInt();
box2 = scanner.nextInt();
box3 = scanner.nextInt();
box4 = scanner.nextInt();
box5 = scanner.nextInt();
box6 = scanner.nextInt();
if (box1+box2+box3+box4+box5+box6==0)
break;
boxNum += box6;
boxNum += box5;
box1 = getMax(0, box1-box5*11);//如果1*1的盒子大于装完5*5盒子 那么box1 的盒子还剩下
boxNum += box4;//装完box4后剩下5个 2*2
if (box2>=box4*5) { //如果装完所有4*4盒子还剩下能装2*2的盒子数量小于现在的2*2盒子
box2 = box2- box4*5; //减去4*4剩下的盒子后 还剩下多少的2*2盒子
}
else {
box1 = getMax(0, box1-4*(box4*5-box2)); //如果2*2的盒子 能被装完4*4后剩下的盒子,剩下的空间就被1*1的盒子装填
box2 = 0;//这个时候 2*2的盒子已经被装完了
}
boxNum +=(box3+3)/4; //四个3*3占一个盒子
box3= box3%4;//还剩下的不满4个3*3盒子
if (box3!=0) { //当箱子放了i个3*3盒子,剩下的空间最多放j个2*2盒子
if (box2>7-2*box3) {
box2-=7-2*box3;
box1=getMax(0, box1-(8-box3));//当箱子放了i个3*3盒子,并尽可能多地放了个2*2盒子后
}
else {
box1 = getMax(0, box1-(36-9*box3-4*box2));
box2 = 0;
}
}
boxNum += (box2+8)/9; //每9个2*2的盒子完全独占一个箱子
box2%=9;//2*2的盒子不足9个时,都放入一个箱子,剩余空间全放1*1
if (box2!=0) {
box1 = getMax(0, box1-(36-4*box2));
}
boxNum += (box1+35)/36; //每36个1*1的盒子完全独占一个箱子
System.out.println(boxNum);
}
scanner.close();
}
private static int getMax (int x,int y)
{
if (x>y)
return x;
return y;
}
}