1017:装箱问题
总时间限制:
1000ms
内存限制:
65536kB
描述
一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为1*1, 2*2, 3*3, 4*4, 5*5, 6*6。这些产品通常使用一个 6*6*h 的长方体包裹包装然后邮寄给客户。因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的包裹数量。他们很需要有一个好的程序帮他们解决这个问题从而节省费用。现在这个程序由你来设计。
输入
输入文件包括几行,每一行代表一个订单。每个订单里的一行包括六个整数,中间用空格隔开,分别为1*1至6*6这六种产品的数量。输入文件将以6个0组成的一行结尾。
输出
除了输入的最后一行6个0以外,输入文件里每一行对应着输出文件的一行,每一行输出一个整数代表对应的订单所需的最小包裹数。
样例输入
0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0
样例输出
2
1
第一种python方法,是逐个填充的方法,一种规格的盒子填充之后,在填充下一种
第二种方法是参考教材《算法基础与在线实践》一书整理成三种语言
# -*- coding:utf-8
pro_list = [int(i) for i in input().split(' ')]
while sum(pro_list):
count = 0
con_list = []
# 处理6×6的:
count += pro_list[5]
# 处理5×5的:
count += pro_list[4]
# 5*5 的还有多少个位置, 然后放入最多的1×1的, 然后5×5的填满了
if pro_list[0] < pro_list[4]*11:
pro_list[0] = 0
else:
pro_list[0] -= pro_list[4]*11
# 处理4×4的,然后先放入2×2的,再1×1的,然后4×4的填满了
count += pro_list[3]
# 每个箱子能放5个2×2的,判断一下有多少个箱子,能放多少个2×2
count_4_2 = pro_list[3]*5
if pro_list[1] < count_4_2:
# 如果放完2×2之后,还有位置,就放入1×1的
# 计算一下还能放多少个1×1的
count_4_1 = 2*(count_4_2 - pro_list[1])
# print(count_4_2, pro_list[1], count_4_1)
if pro_list[0] < count_4_1:
pro_list[0] = 0
else:
pro_list[0] -= count_4_1
pro_list[1] = 0
# print(pro_list[0])
else:
pro_list[1] -= count_4_2
# 处理3×3的
if pro_list[2] % 4 == 0:
count += int(pro_list[2]/4)
else:
count += int(pro_list[2] / 4)
count += 1
if pro_list[2] % 4 == 1: # 剩余一个箱子,可以放入最多5个2×2的,和7个1×1的,然后填满了
if pro_list[1] < 5:
pro_list[1] = 0
else:
pro_list[1] -= 5
if pro_list[0] < 7:
pro_list[0] = 0
else:
pro_list[0] -= 7
elif pro_list[2]%4 == 2: # 剩余两个箱子,可以放入最多3个2×2的,和6个1×1的,然后填满了
if pro_list[1] < 3:
pro_list[1] = 0
else:
pro_list[1] -= 3
if pro_list[0] < 6:
pro_list[0] = 0
else:
pro_list[0] -= 6
else: # 剩余3个箱子, 可以最多放入1个2×2的和5个1×1的,然后就填满了
if pro_list[1] < 1:
pro_list[1] =0
else:
pro_list[1] -= 1
if pro_list[0] < 5:
pro_list[0] = 0
else:
pro_list[0] -= 5
# 处理2×2的,多余的用1×1填充,可以填入36-2×2×n_4
count += int(pro_list[1] / 9)
if pro_list[1] % 9 == 0:
pro_list[1] = 0
else:
count += 1
count_9 = pro_list[1]%9
count_9_1 = 36-count_9*4
if pro_list[0] < count_9_1:
pro_list[0] = 0
else:
pro_list[0] -= count_9_1
# 处理1×1 的,对36取余数即可
# print(pro_list[0])
count += pro_list[0] / 36
if pro_list[0] % 36 is not 0:
count += 1
print(int(count))
pro_list = [int(i) for i in input().split(' ')]
C++
#include<iostream>
using namespace std;
int main()
{
int n, a, b, c, d, e, f, x, y;
//x 表示1×1的空位数目
//y 表示2×2的空位数目
int u[4]={0, 5, 3, 1};
//表示3×3的产品分别是4k, 4k+1, 4k+2, 4k+3时
//为3×3产品打开的新箱子中,剩余2×2的空位数
while(scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f)!=EOF){
if(a+b+c+d+e+f == 0)break;
n = f+e+d+c/4; //至少要多少个箱子
if(c%4!=0)
n++;
y = 5*d + u[c%4];
if(b>y) {
n += (b - y) / 9;
if ((b - y) % 9 != 0)
n++;
}
x = 36*n - 36*f - 25*e - 16*d - 9*c - 4*b; //x是现在所有的箱子中,剩下的1×1的位置
if(a>x)
{
n += (a-x)/36;
if ((a-x)%36!=0)
n++;
}
printf("%d\n", n);
}
return 0;
}
java
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int a,b,c,d,e,f,x,y,n;
a = s.nextInt();
b = s.nextInt();
c = s.nextInt();
d = s.nextInt();
e = s.nextInt();
f = s.nextInt();
int [] u ={0, 5, 3, 1};
while (a+b+c+d+e+f!=0){
n = f+e+d+c/4;
if (c%4 != 0){
n++;
}
y = 5*d + u[c%4];
if (b>y){
n += (b-y)/9;
if((b-y)%9!=0){
n++;
}
}
x = 36*n - 36*f - 25*e - 16*d - 9*c - 4*b;
if (a>x){
n += (a-x)/36;
if ((a-x)%36!=0){
n++;
}
}
System.out.println(n);
a = s.nextInt();
b = s.nextInt();
c = s.nextInt();
d = s.nextInt();
e = s.nextInt();
f = s.nextInt();
}
}
}
python3
# -*-coding:utf-8 -*-
pro_list = [int(i) for i in input().split(' ')]
count_3_2 = [0, 5, 3, 1]
while sum(pro_list):
n = pro_list[5]+pro_list[4]+pro_list[3]+int(pro_list[2]/4)
if pro_list[2]%4 is not 0:
n += 1
y = 5*pro_list[3] + count_3_2[pro_list[2]%4]
if pro_list[1]>y:
n += int((pro_list[1]-y)/9)
if (pro_list[1]-y)%9 is not 0:
n += 1
x = 36*n - 36*pro_list[5] - 25*pro_list[4] - 16*pro_list[3] - 9*pro_list[2] - 4*pro_list[1]
if pro_list[0]>x:
n += int((pro_list[0]-x)/36)
if (pro_list[0]-x)%36 is not 0:
n += 1
print(n)
pro_list = [int(i) for i in input().split(' ')]