【C++】Packets

 

                                                    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 

 

思路:

参考:点击打开链接

6*6  的产品每个会占用一个完整的箱子,并且没有空余空间;

5*5 的产品每个占用一个新的箱子,并且留下 11 个可以盛放 1*1的产品的空余空间;

4*4 的产品每个占用一个新的箱子,并且留下 5 个可以盛放 2*2 的产品的空余空间;

3*3的产品最复杂:3*3 的产品另开新的箱子,新开的箱子数目等于 3*3 的产品的数目除以4 向上取整;

                                 需要讨论为 3*3 的产品新开箱子时,剩余的空间可以盛放多少 2*2 和1*1 的产品(这里如果有空间可以盛放 2*2 的产品,我们就将它计入 2*2 的空余空间,等到2*2 的产品全部装完,如果还有 2*2 的空间剩余,再将它们转换成 1*1 的剩余空间)

                                  1. 3*3 的产品的数目正好是 4 的倍数,所以没有空余空间;

                                  2. 3*3 的产品数目是 4 的倍数加 1,这时还剩 5 个 2*2 的空位和 7 个 1*1 的空位;

                                  3. 3*3 的产品数目是 4 的倍数加 2,这时还剩 3 个 2*2 的空位和 6 个 1*1 的空位;

                                  4. 3*3 的产品数目是 4 的倍数加 3,这时还剩 1 个 2*2 的空位和 5 个 1*1 的空位;

剩余的 2*2的空位和 2*2 产品的数目,如果产品数目多,就将 2*2 的空位全部填满,再为 2*2 的产品打开新箱子,同时计算新箱子中 1*1 的空位,如果剩余空位多,就将 2*2 的产品全部填入 2*2的空位,再将剩余的 2*2 的空位转换成 1*1 的空位;

最后处理 1*1 的产品,比较一下 1*1的空位与 1*1 的产品数目,如果空位多,将 1*1 的产品全部填入空位,否则,先将 1*1 的空位填满,然后再为 1*1 的产品打开新的箱子。

 

我自己的一些理解:

               因为填充空隙时,均用2*2和1*1的箱子,所以从一开始从0开始计数,计算直至填满了3*3箱子后,需要的2*2和1*1的数目,记为count_1和count_2 。再与输入的数目比较,如果小于输入,自然就不需要再增加包裹。如果大于,则再计算。

 

ac代码:

#include<iostream>
using namespace std;
int Three_2[4]={0,5,3,1};//装有3*3的包裹可放2*2的个数,3*3 %4 依次剩0,1,2,3个
int Three_1[4]={0,7,6,5};//装有3*3的包裹可放1*1的个数,3*3 %4 依次剩0,1,2,3个
int main()
{
    int box[7];//i=1,所以为7
    int sumbox;
    int count_1,count_2;
    int ans;
    while(1)
    {
        sumbox=0;
        for(int i=1;i<=6;i++)
        {
            cin>>box[i];
            sumbox+=box[i];
        }
        if(sumbox==0) break;

        ans=box[6]+box[5]+box[4]+(box[3]+3)/4;//+3 防止box[3]<4
        count_2=box[4]*5+Three_2[box[3]%4]; //2*2可放入4*4中和放入3*3中的个数
        count_1=box[5]*11+Three_1[box[3]%4]; //1*1可放入5*5中和放入3*3中的个数

        if(count_2<box[2])//如果不够放2*2。 开辟新的包裹
        {
            ans+=(box[2]-count_2+8)/9;//+8 防止剩下的box[2]<9,等式右边:新开包裹数
            count_1+=4*(9*  ((box[2]-count_2+8)/9)-(box[2]-count_2)  );//开新包裹剩下的地方装1*1
        }
        else
            count_1+=4*(count_2-box[2]);//刚好够放2*2或者有多余空间时,放1*1的箱子
        if(count_1<box[1])//如果不够放1*1。 开辟新的包裹
            ans+=(box[1]-count_1+35)/36;//+35 防止剩下的box[1]<36

        cout<<ans<<endl;
    }
    return 0;
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值