贪心算法!!
~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.
一个工厂制造的产品形状都是长方体,它们的高度都是 h,长和宽都相等,一共有六个
型号,他们的长宽分别为 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. 这些产品通常使用一个 6*6*h 的长方
体包裹包装然后邮寄给客户。因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的
包裹数量。他们很需要有一个好的程序帮他们解决这个问题从而节省费用。现在这个程序由
你来设计。
~
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.
输入文件包括几行,每一行代表一个订单。每个订单里的一行包括六个整数,中间用空格隔开,分别为1*1至6*6这六种产品的数量。输入文件将以6个0组成的一行结尾。
~
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.
除了输入的最后一行6个0以外,输入文件里每一行对应着输出文件的一行,每一行输出一个整数代表对应的订单所需的最小包裹数。
Sample Input
0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0
Sample Output
2
1
代码:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int b1,b2,b3,b4,b5,b6,n,n1,n2;
int b[4]={0,5,3,1};
while(scanf("%d%d%d%d%d%d",&b1,&b2,&b3,&b4,&b5,&b6)!=EOF)
{
if(b1==0&&b2==0&&b3==0&&b4==0&&b5==0&&b6==0)
break;
n=b6+b5+b4+(b3+3)/4;
n2=5*b4+b[b3%4];
if(b2>n2)
n+=(b2-n2+8)/9;
n1=36*(n-b6)-25*b5-16*b4-9*b3-4*b2;
if(b1>n1)
n+=(b1-n1+35)/36;
cout<<n<<endl;
}
return 0;
}
思路:
首先分析输入:
第一组表示有 4 个 3*3 的产品和一个 6*6 的产品,此时 4 个 3*3 的产品占用一个箱子,另外
一个 6*6 的产品占用 1 个箱子,所以箱子数是 2;第二组表示有 7 个 1*1 的产品,5 个 2*2
的产品和 1 个 3*3 的产品,我们可以把他们统统放在一个箱子中,所以输出是 1。