ZOJ 1307 Packets - 贪心

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 

题意:有六种型号的产品,规格分别为1*1,2*2,3*3,4*4,5*5,6*6,高度均为h。现在要把他们装在一个6*6的箱子里,箱子高度也为h,问最少用几只箱子。

 

思路:先从6*6的产品开始考虑,刚好放满一个箱子;再考虑5*5,放一个5*5以后还能放11个1*1;再考虑4*4,放一个4*4后还能放5个2*2;再考虑3*3,此时问题比较复杂,分四种情况:放一个3*3,那么可以放5个2*2,7个1*1;放2个3*3,那么可以放3个2*2,6个1*1;放三个3*3,那么可以放1个2*2,5个1*1;最后再看2*2是否能放完,不能就再开一个箱子,可以的话记录下箱子的空位以放1*1。

 

源代码:

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int n,a,b,c,d,e,f,x,y,n_b;//x是可以存几个2*2,y是可以存几个1*1
 7     int s_2[4]={0,5,3,1};//3*3的可以放几个2*2
 8     int s_1[4]={0,7,6,5};//3*3的可以放几个1*1
 9     while(1)
10     {
11         scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f);
12         if(a+b+c+d+e+f==0) break;
13         n=f+e+d+(c+3)/4;//  (c+3)/4向上取整
14         x=d*5+s_2[c%4];
15         if(x<b)//已有的箱子不够放2*2
16             {
17             n_b=(b-x+8)/9;//还需的箱子
18             n=n_b+n;
19             y=e*11+s_1[c%4]+n_b*36-(b-x)*4;
20             }
21             else
22             y=e*11+s_1[c%4]+(x-b)*4;
23         if(y<a) //剩余的空位不够放1*1
24           n+=(a-y+35)/36;
25     printf("%d\n",n);
26     }
27     return 0;
28 }

 

posted @ 2013-08-06 00:18  小の泽  阅读(473)  评论(0编辑  收藏  举报