poj 1017 Packets

C语言题库

共 3885字,需浏览 8分钟

 · 2021-10-20

Packets


Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 73338
Accepted: 25083

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。这些产品总是以与产品相同的高度h,尺寸为6*6的方形包裹发送给客户。因为费用是工厂和客户的利益,尽量减少必要的包裹数量,以交付订购的产品从工厂到客户。一个好的程序可以解决根据订单找到运送给定产品所需的最小数量包裹的问题,这会节省很多钱。你被要求制作这样一个程序。


输入

输入文件由几行指定订单组成。每一行指定一个订单。顺序由6个整数描述,每个整数由一个空格隔开,表示从最小尺寸1*1到最大尺寸6*6的包的数量。输入文件的末尾由包含6个零的行表示。


输出

输出文件中每一行对应输入文件中的一行。这一行包含最小数量的包,可以将来自输入文件相应行的订单打包到其中。在输出文件中没有一行对应于输入文件的最后一个“null”行。

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 的长方体箱子包装然后邮寄给客户。因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的箱子数量BoxNum。


解题思路:


由于盒子和箱子的高均为h,因此只需考虑底面积的空间。

6*6的盒子,每个盒子独占一个箱子。


5*5的盒子,每个盒子放入一个箱子,该箱子的剩余空间允许放入的最大尺寸为1*1,且最多放11个。


4*4的盒子,每个盒子放入一个箱子,该箱子的剩余空间允许放入的最大尺寸为2*2。


3*3的盒子,每4个刚好独占一个箱子,不足4个3*3的,剩下空间由2*2和1*2填充。


2*2的盒子和1*1的盒子主要用于填充其他箱子的剩余空间,填充后的多余部分才开辟新箱子装填。

详细解题思路看程序注释。


代码:

#include
using namespace std;

int max(int a,int b)
{
return a>b?a:b;
}

int main(void)
{
int s1,s2,s3,s4,s5,s6; //6种size的盒子数量
while(cin>>s1>>s2>>s3>>s4>>s5>>s6 && (s1+s2+s3+s4+s5+s6))
{
int BoxNum=0; //放进所有盒子所需的最少箱子数

BoxNum+=s6; //6*6的盒子,每个都刚好独占一个箱子

BoxNum+=s5; //5*5的盒子,放进箱子后,每个箱子余下的空间只能放11个1*1的盒子
s1=max(0,s1-s5*11); //把1*1的盒子尽可能地放进已放有一个5*5盒子的箱子

BoxNum+=s4; //4*4的盒子,放进箱子后,每个箱子余下的空间为5个2*2的盒子空间
//先把所有2*2的盒子尽可能地放进这些空间
if(s2>=s4*5) //若2*2的盒子数比空间多
s2-=s4*5; //则消去已放进空间的部分
else //若2*2的盒子数比空间少
{ //则先把所有2*2的盒子放进这些空间
s1=max(0,s1-4*(s4*5-s2)); //再用1*1的盒子填充本应放2*2盒子的空间
s2=0; //一个2*2空间可放4个1*1盒子
}

BoxNum+=(s3+3)/4; //每4个3*3的盒子完全独占一个箱子
s3%=4; //3*3的盒子不足4个时,都放入一个箱子,剩余空间先放2*2,再放1*1
if(s3)
{ //当箱子放了i个3*3盒子,剩下的空间最多放j个2*2盒子
if(s2>=7-2*s3) //其中i={1,2,3} ; j={5,3,1} 由此可得到条件的关系式
{
s2-=7-2*s3;
s1=max(0,s1-(8-s3)); //当箱子放了i个3*3盒子,并尽可能多地放了个2*2盒子后
} //剩下的空间最多放j个1*1盒子,其中i={1,2,3} ; j={7,6,5}
else //但当2*2的盒子数不足时,尽可能把1*1盒子放入剩余空间
{ //一个箱子最多放36个1*1,一个3*3盒子空间最多放9个1*1,一个2*2盒子空间最多放4个1*1
s1=max(0,s1-(36-9*s3-4*s2)); //由此很容易推出剩余空间能放多少个1*1
s2=0;
}
}

BoxNum+=(s2+8)/9; //每9个2*2的盒子完全独占一个箱子
s2%=9; //2*2的盒子不足9个时,都放入一个箱子,剩余空间全放1*1
if(s2)
s1=max(0,s1-(36-4*s2));

BoxNum+=(s1+35)/36; //每36个1*1的盒子完全独占一个箱子

cout< }
return 0;
}


浏览 7
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报