poj 1017 Packets

来源:互联网 发布:小米手环2无睡眠数据 编辑:程序博客网 时间:2024/05/03 15:42

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的货物(如果2×2的货物不够,可以用1×1的货物填充剩余的空间)而对于3×3的货物,每箱可以放四件,最后可能会剩余1,2,3件,则分别可以填充的2×2的货物为5,3,1件,剩余的空间以及2×2货物不足的情况下用1×1的填充。然后再考虑对于2×2的货物,每箱可以放9件,空出来的空间里放1×1的货物。

整个程序不需要循环,十分简单。看过高手的代码,相比之下,我写的代码还是比较丑陋,废话少说,上代码。

 

/* * poj1017main.cpp * *  Created on: 2012-4-28 *      Author: wwf */#include<iostream>using namespace std;int solve(int x1, int x2, int x3, int x4, int x5, int x6);int main(void){int x1,x2,x3,x4,x5,x6;while(true){cin>>x1>>x2>>x3>>x4>>x5>>x6;if(x1==0&&x2==0&&x3==0&&x4==0&&x5==0&&x6==0){break;}else{cout<<solve(x1,x2,x3,x4,x5,x6)<<endl;}}return 0;}int solve(int x1, int x2, int x3, int x4, int x5, int x6){int result=0;result+=x6;result+=x5;x1-=x5*11;if(x1<0){x1=0;}result+=x4;if(x2<5*x4){//2*2 squares are not enoughx1-=(5*x4-x2)*4;if(x1<0){x1=0;}x2=0;}else{x2-=5*x4;}result+=x3/4;x3%=4;if(x3==1){//5个x2,7个x1result+=1;if(x2<5){x1-=(7+4*(5-x2));if(x1<0)x1=0;x2=0;}else{x2-=5;x1-=7;if(x1<0)x1=0;}}else if(x3==2){//3个x2,6个x1result+=1;if(x2<3){x1-=(6+4*(3-x2));if(x1<0)x1=0;x2=0;}else{x2-=3;x1-=6;if(x1<0)x1=0;}}else if(x3==3){//1个x2,5个x1result+=1;if(x2==0){x1-=(5+4);if(x1<0)x1=0;}else{x2-=1;x1-=5;if(x1<0)x1=0;}}result+=x2/9;x2%=9;if(x2!=0){result+=1;x1-=(9-x2)*4;if(x1<0)x1=0;}result+=x1/36;x1%=36;if(x1>0)result++;return result;}


 

 

 

 

 

原创粉丝点击