zoukankan      html  css  js  c++  java
  • POJ 1017Packets(贪心)

    Packets
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 40544   Accepted: 13594

    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 


    题目大意:做出来的产品高度为h,长宽一样,有1*1,2*2,3*3,4*4,5*5,6*6,六种规格,把他们装进6*6高h的箱子中,问你最少需要多少箱子。

      解题思路:6*6的直接填进去,5*5的还可以放11个1*1的,4*4的可以放5个2*2的2*2的用完了,可以放1*1的,3*3的每四个放一个箱子里,如果还剩的有,那就分情况讨论,往里面塞2*2和1*1的。最后只剩下2*2和1*1就可与随便装了!具体见代码。

      题目地址:Packets

    AC代码:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    
    int main()
    {
        int a,b,c,d,e,f;
        int res;
        while(scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f))
        {
            if(!a&&!b&&!c&&!d&&!e&&!f) break;
            res=f;  //6*6的直接填满
            res+=e;
            if(a<=e*11) a=0; //一个5*5的可以再塞11个1*1的
            else a-=e*11;
            res+=d;
            if(b>d*5) b-=d*5;
            else   //2*2的用完了
            {
                int tt=(d*5-b)*4;
                b=0;
                if(a>tt) a=a-tt;  //放1*1的
                else a=0;
            }
    
            res+=c/4;  //每四个3*3组成1个6*6
            int tmp=c%4;
            if(tmp)  //表示还有剩余3*3的
            {
                res++;
                if(tmp==1)
                {
                    if(b>5)
                    {
                        b-=5;  //放2*2
                        if(a>7) a-=7;  //放1*1
                        else a=0;
                    }
                    else
                    {
                        int tt=(5-b)*4;
                        b=0;
                        tt+=7;
                        if(a>tt) a-=tt;
                        else a=0;
                    }
                }
                else if(tmp==2)
                {
                    if(b>3)
                    {
                        b-=3;  //放2*2
                        if(a>6) a-=6;  //放1*1
                        else a=0;
                    }
                    else
                    {
                        int tt=(3-b)*4;
                        b=0;
                        tt+=6;
                        if(a>tt) a-=tt;
                        else a=0;
                    }
                }
                else if(tmp==3)
                {
                    if(b>1)
                    {
                        b-=1;  //放2*2
                        if(a>5) a-=5;  //放1*1
                        else a=0;
                    }
                    else
                    {
                        int tt=(1-b)*4;
                        b=0;
                        tt+=5;
                        if(a>tt) a-=tt;
                        else a=0;
                    }
                }
            }
    
            tmp=a+b*4;   //到最后全是1*1和2*2的就可以直接填了
            res+=tmp/36;
            if(tmp%36)
                res++;
            printf("%d
    ",res);
        }
        return 0;
    }
    
    //596 21 348 9 8 4
    



  • 相关阅读:
    xml转换为json格式时,如何将指定节点转换成数组 Json.NET
    快速删除C#代码中的空白行
    C#编程中的Image/Bitmap与base64的转换及 Base-64 字符数组或字符串的长度无效问题 解决
    Flash设置(各种版本浏览器包括低版本IE)
    使用vcastr22.swf做flash版网页视频播放器
    使用VLC Activex插件做网页版视频播放器
    web项目 在visual studio 输出窗口显示调试信息
    geos 3.6.3库windows版本 已编译完成的32位版本和64位版本
    vs2017 打开附带的localdb v13
    visual studio code 里调试运行 Python代码
  • 原文地址:https://www.cnblogs.com/phisy/p/3371917.html
Copyright © 2011-2022 走看看