• 装箱问题(贪心)


    Packets
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 28329   Accepted: 9291

    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 

    Source

     
    【题意】:

    一个工厂制造的产品形状都是长方体盒子,它们的高度都是 h,长和宽都相等,一共有六个型号,分别为1*1, 2*2, 3*3, 4*4, 5*5, 6*6。

    这些产品通常使用一个 6*6*h 的长方体箱子包装然后邮寄给客户。因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的箱子数量BoxNum.

    【思路】:我tm一直以为是先装小的啊啊啊,对啊,能装尽量装,把尽量多的产品放进去不就避免多开箱子,放尽量多的产品不就先放小的的么????然后我就不会做了,网上先放大的当时我???,然后推了推,如果 是8个1*1,和一个5*5,如果按我先放小的,那样就放不进5*5了,如果先放5*5,就可以放进去8个1*1了。。。。所以千万不要理所当然。。。(当然如果你一开始跟我一样想的话QAQ)

    【代码君】

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 using namespace std;
     5 int room2[4]={0,5,3,1};
     6 int main()
     7 {    
     8     int s1,s2,s3,s4,s5,s6,n1,n2;
     9     while(scanf("%d%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5,&s6)&&s1||s2||s3||s4||s5||s6)//能够输入并且都不为0时 
    10     {
    11         int sum=0;//计算箱子总数0 
    12         sum+=s6+s5+s4+(s3+3)/4;//6*6和5*5和4*4一定是一个占一个箱子,而3*3 4个占一个箱子 
    13         n2=5*s4+room2[s3%4];//剩余的能够放2*2的空间,有5*5,6*6的箱子已经不能放2*2了,放了4*4的箱子可以放5个2*2+放3*3的箱子对应的2*2的空间 
    14         if(s2>n2)//2*2的个数比我们留出来为2*2的空间个数多,就需要为2*2另开箱子 
    15         {
    16             sum+=(s2-n2+8)/9;//求出多需要几个2*2空间,+8是向上取整,再除以9,(因为每个箱子可以放9个2*2) 
    17         }
    18         n1=36*(sum-s6)-s2*2*2-s3*3*3-s4*4*4-s5*5*5;//用减法计算1*1剩余的空间 
    19         if(s1>n1)//如果1*1的个数,比我们留出的空间多就需要另开空间 
    20         sum+=(s1-n1+35)/36;//+35是向上取整,将多出来的另开箱子 
    21         printf("%d
    ",sum);
    22     }
    23     return 0;
    24 }
  • 相关阅读:
    Java User Thread and Daemon Thread
    BFS 和 DFS
    fail-fast vs fail-safe iterator in Java
    通过先序遍历和中序遍历后的序列还原二叉树
    单例模式总结
    TCP性能陷阱
    数据库事务的四大特性和事务隔离级别
    深入理解Java虚拟机- 学习笔记
    字符串,引用变量与常量池
    深入理解Java虚拟机- 学习笔记
  • 原文地址:https://www.cnblogs.com/zzyh/p/6644754.html
Copyright © 2020-2023  润新知