题目链接:http://poj.org/problem?id=1017
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
题目大意:将给定类型的产品全部放入6*6的箱子中, 需要6*6箱子最少数目。
首先确定解题思路:贪心。
分析产品放置组合:
如果一个箱子中放置了一个6*6的产品,则此箱子中无法放置其它产品;若放置一个5*5的产品,则此箱子中还可放置11个1*1的产品;若放4*4的产品,还可放5个2*2的产品。若放置3*3的产品,可以放置4个、3个、2个、1个,剩余的空位分别对应可以放置0个1*1、0个2*2,5个1*1、1个2*2,6个1*1、3个2*2,7个1*1、5个2*2;若放2*2产品,可放9个;若放1*1产品,可放36个。
考虑时从大到小放置,先放大产品,再放小产品。根据分析可知:已知各型号产品数分别为:n1、n2、n3、n4、n5、n6。
6*6产品需要n6个箱子,5*5产品需要n5个箱子,4*4产品需要n4个箱子,对于3*3的产品,需要进一步考虑是否能被4整除,若n3%4==0,则需n3/4个箱子,否则还需要一个箱子。由于前面的箱子中留有1*1或2*2的空位,只需要将1*1和2*2的产品尽量填充空位即可,当空位不够用时,在增加箱子。
具体代码如下:
1 #include<iostream>
2 using namespace std;
3 int main()
4 {
5 int num[7];
6 int count;
7 int i,f=0;
8 for(i=1;i<=6;i++)
9 {cin>>num[i];f+=num[i];}
10 while(f){
11 count=0;f=0;
12 count=count+num[6]+num[5]+num[4]+(num[3]+3)/4;
13 int n1,n2;
14 n1=11*num[5];
15 n2=5*num[4];
16 switch(num[3]%4){
17 case 0:break;
18 case 1:n1+=7;n2+=5;break;
19 case 2:n1+=6;n2+=3;break;
20 default :n1+=5;n2+=1;
21 }
22 if(num[2]-n2<=0) n1+=4*(n2-num[2]);
23 else {
24 if((num[2]-n2)%9){
25 count+=((num[2]-n2)/9+1);
26 n1+=(36-(num[2]-n2)%9*4);
27 }
28 else count+=(num[2]-n2)/9;
29
30 }
31 if(num[1]-n1>0)
32 {
33 if((num[1]-n1)%36)
34 count+=((num[1]-n1)/36+1);
35 else count+=(num[1]-n1)/36;
36 }
37 cout<<count<<endl;
38 for(i=1;i<=6;i++)
39 {
40 cin>>num[i]; f+=num[i];
41 }
42 }
43 return 0;
44 }
2 using namespace std;
3 int main()
4 {
5 int num[7];
6 int count;
7 int i,f=0;
8 for(i=1;i<=6;i++)
9 {cin>>num[i];f+=num[i];}
10 while(f){
11 count=0;f=0;
12 count=count+num[6]+num[5]+num[4]+(num[3]+3)/4;
13 int n1,n2;
14 n1=11*num[5];
15 n2=5*num[4];
16 switch(num[3]%4){
17 case 0:break;
18 case 1:n1+=7;n2+=5;break;
19 case 2:n1+=6;n2+=3;break;
20 default :n1+=5;n2+=1;
21 }
22 if(num[2]-n2<=0) n1+=4*(n2-num[2]);
23 else {
24 if((num[2]-n2)%9){
25 count+=((num[2]-n2)/9+1);
26 n1+=(36-(num[2]-n2)%9*4);
27 }
28 else count+=(num[2]-n2)/9;
29
30 }
31 if(num[1]-n1>0)
32 {
33 if((num[1]-n1)%36)
34 count+=((num[1]-n1)/36+1);
35 else count+=(num[1]-n1)/36;
36 }
37 cout<<count<<endl;
38 for(i=1;i<=6;i++)
39 {
40 cin>>num[i]; f+=num[i];
41 }
42 }
43 return 0;
44 }