js-DOM



 Packets 

A factory produces products packed in square packets of the same height h and of the sizes  ,  , ,  ,  ,  . These products are always delivered to customers in the square parcels of the same height h as the products have and of the size  . 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  to the biggest size  . 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

题意:有一间工厂生产的东西, 被包装在相同高度h 的正方形容器内, 但其面积大小分别有:1*1, 2*2, 3*3, 4*4, 5*5, 6*6等六种尺寸。这些产品总是用高度为h,面积为6*6的箱子打包后寄给客户。因为成本关系,当然希望将客户所订购的产品放在最少的箱子里寄出。请你写一个程式找出寄送这些产品最少需要多少个箱子,这可以使工厂节省下不少钱。

思路:很典型的贪心思路了,首先6*6的盒子只能单独放箱子里,5*5的盒子可以和11个1*1的盒子搭配。4*4的盒子优先和2*2的盒子搭配,如果2*2的盒子没了再与1*1的盒子搭配,3*3的盒子优先与2*2盒子搭配,再与1*1的盒子搭配,这里要分好几种情况。2*2的盒子只能与1*1的盒子搭配。想了好久,一开始想错了,代码写的也很烂。。

代码如下:

#include<iostream>
#include<algorithm>

using namespace std;

int arry[10];

int main()
{
    while(cin>>arry[1]>>arry[2]>>arry[3]>>arry[4]>>arry[5]>>arry[6])
    {
        if(arry[1]==0&&arry[2]==0&&arry[3]==0&&arry[4]==0&&arry[5]==0&&arry[6]==0) break;
        int sum=arry[6];
        while(arry[5]>0) //大小为5*5的只能与1*1的搭配
        {
            sum++;
            arry[5]--;
            if(arry[1]>=11) arry[1]=arry[1]-11;
            else if(arry[1]<11&&arry[1]>0) arry[1]=0;
        }
        while(arry[4]>0)
        {
            sum++;
            arry[4]--;
            if(arry[2]>0) //4*4只能与2*2,1*1搭配,当2*2的还有时优先与2*2的搭配
            {
                if(arry[2]>=5) arry[2]=arry[2]-5;
                else if(arry[2]<5)
                {
                    int remain=20-4*arry[2];
                    arry[2]=0;
                    if(arry[1]>0) //当2*2不足时与1*1搭配
                    {
                        if(arry[1]<remain) arry[1]=0;
                        else arry[1]=arry[1]-remain;
                    }
                }
            }
            else //当2*2不足时与1*1搭配
            {
                if(arry[1]>=20) arry[1]=arry[1]-20;
                else arry[1]=0;
            }
        }
        while(arry[3]>0) //3*3的搭配种类比较多而且较为麻烦,要分别考虑能放下多少个3*3的盒子
        {
            sum++;
            int remain,num;
            if(arry[3]>=4) arry[3]=arry[3]-4,remain=0,num=4;
            else remain=36-arry[3]*9,num=arry[3],arry[3]=0;
            if(num==4) continue;
            else if(num==1)
            {
                if(arry[2]>=5) //先与2*2搭配
                {
                    arry[2]=arry[2]-5;
                    remain=remain-20;
                    if(remain<arry[1]) arry[1]=arry[1]-remain;//再与1*1搭配
                    else arry[1]=0;
                }
                else //与1*1搭配
                {
                    remain=remain-arry[2]*4;
                    arry[2]=0;
                    if(remain>=arry[1]) arry[1]=0;
                    else arry[1]=arry[1]-remain;
                }
            }
            else if(num==2)
            {
                if(arry[2]>=3)
                {
                    arry[2]=arry[2]-3;
                    remain=remain-12;
                    if(remain<arry[1]) arry[1]=arry[1]-remain;
                    else arry[1]=0;
                }
                else
                {
                    remain=remain-arry[2]*4;
                    arry[2]=0;
                    if(remain>=arry[1]) arry[1]=0;
                    else arry[1]=arry[1]-remain;
                }
            }
            else if(num==3)
            {
                if(arry[2]>=1)
                {
                    arry[2]=arry[2]-1;
                    remain=remain-4;
                    if(remain<arry[1]) arry[1]=arry[1]-remain;
                    else arry[1]=0;
                }
                else
                {
                    remain=remain-arry[2]*4;
                    arry[2]=0;
                    if(remain>=arry[1]) arry[1]=0;
                    else arry[1]=arry[1]-remain;
                }
            }
        }
        while(arry[2]>0) //2*2的盒子只能与1*1的盒子搭配。
        {
            sum++;
            if(arry[2]>=9) arry[2]=arry[2]-9;
            else
            {
                int remain=36-arry[2]*4;
                arry[2]=0;
                if(remain>arry[1]) arry[1]=0;
                else arry[1]=arry[1]-remain;
            }
        }
        while(arry[1]>0)
        {
            sum++;
            if(arry[1]>=36) arry[1]=arry[1]-36;
            else arry[1]=0;
        }
        cout<<sum<<endl;
    }
    return 0;
}

js-DOM,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。