Packets

Packets

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.先装6 * 6的产品一个箱子装一个就满
2.装完所有6 * 6的产品后,考虑55的产品,一个箱子装完55的产品后,只能再用1 * 1的产品填补空隙。
3.装完所有5 * 5的产品后,开始装4 * 4的产品,装完4 * 4的产品后可以先考虑用2 * 2的产品填补空隙,2 * 2的产品用完后,再考虑11的产品填补空隙
4.装完所有4 * 4的产品后,装3 * 3的产品,装完3 * 3的产品后,可以再装2 * 2的产品,但此时要画图讨论最多能装2
2产品的数量.
只装1个3 * 3产品时,最多装5个2 * 2产品,如图
在这里插入图片描述
只装2个3 * 3产品时,最多装3个2 * 2产品,如图
在这里插入图片描述
只装3个3 * 3产品时,最多装1个2 * 2产品,如图
在这里插入图片描述
再考虑1 * 1的产品填补空隙。
5.装完2 * 2的产品后,由于比2 * 2大的尺寸都已经装过不考虑,只考虑用2 * 2和1 * 1的产品来填补空隙。
6.将剩下的1 * 1的产品装箱
完整代码:

#include<iostream>
using namespace std;
int main(void){
   int a,b,c,d,e,f;
   int num;
   int area;//剩余面积
   while(cin>>a>>b>>c>>d>>e>>f,a+b+c+d+e+f!=0){
       num=0;
       while(f>0){
           num++;
           f--;
       }//6*6的产品一个箱子只能装一个
       while(e>0){
           area=6*6-5*5;
           e--;
           while(a>0&&area>=1){
               a=a-1;
               area=area-1;
           }
           num++;
       }//装完5*5的产品后,只能再装1*1的产品
       while(d>0){
           area=6*6-4*4;
           d--;
           while(b>0&&area>=4){
               b--;
               area=area-4;
           }
           while(a>0&&area>=1){
               a--;
               area=area-1;
           }
           num++;
       }//装完4*4的产品后,可以再装2*2,1*1的产品,先装2*2的产品,再装1*1的产品,可以将空间利用率最大化
       while(c>0){
           area=6*6-3*3;
           c--;
           while(c>0&&area>=9){
               c--;
               area=area-9;
           }
           if(area==6*6-3*3){
               while(b>0&&area>6*6-3*3-2*2*5){
                   b--;
                   area=area-4;
               }
           }
           else if(area==6*6-3*3*2){
               while(b>0&&area>6*6-3*3*2-2*2*3){
                   b--;
                   area=area-4;
               }
           }
           else if(area==6*6-3*3*3){
               while(b>0&&area>6*6-3*3*3-2*2*1){
                   b--;
                   area=area-4;
               }
           }
           while(a>0&&area>=1){
               a--;
               area=area-1;
           }
           num++;
       }/*装完3*3的产品后,可以再装3*3,2*2,1*1的产品,先装3*3的产品,再装2*2的产品,但2*2的产品情况比较复杂
       最后装1*1的产品,可以将空间利用率最大化*/
        while(b>0){
            area=6*6-2*2;
            b--;
            while(b>0&&area>=4){
                b--;
                area=area-4;
            }
            while(a>0&&area>=1){
                a--;
                area=area-1;
            }
            num++;
        }//装完2*2的产品后,由于比2*2大的尺寸都已经装过不考虑,只考虑2*2和1*1的尺寸
        while(a>0){
            area=6*6-1*1;
            a--;
            while(a>0&&area>=1){
                a--;
                area=area-1;
            }
            num++;
        }
        cout<<num<<endl;
   }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值