poj1017 packets

来源:互联网 发布:嗟乎时运不齐命途多舛 编辑:程序博客网 时间:2024/06/06 02:04
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


解题思路:这是一道装箱问题,每次装的时候装最大箱子,将1和2往大箱子里面塞,首先6*6必须装一个箱子,5*5类型的也必须装一个箱子
4*4也需要装一个箱子,对于3*3问题的要分情况考虑,首先要向上取整;装5*5箱子的时候还可以装11个1*1的箱子,装4*4箱子的时候还可
以装5个2*2箱子;对于3*3的箱子向上取整如果是4的倍数多1的时候,剩下的空间还可以装5个2*2的7个1*1的;如果是4的倍数多2的时候,
剩下的空间还可以装3个2*2,6个1*1;如果是4的倍数多3,剩下的空间还可以装1个2*2,5个1*1;vis数组里面存的数可以装1类型的,2类型
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int main(){    int a,b,c,d,e,f;    int vis[10];    int sum;    while(cin>>a>>b>>c>>d>>e>>f)    {        sum=0;        memset(vis,0,sizeof(vis));        if(a+b+c+d+e+f==0)            break;            sum+=f;            if(e!=0)            {                sum+=e;        //箱子类型为5的话总数加1                vis[1]+=e*11;            }            if(d!=0)            {                sum+=d;        //箱子类型为4的话总数加1                vis[2]+=d*5;            }            if(c!=0)            {                sum+=(c+3)/4;      //向4进1,使箱子数加1                vis[3]+=4-c%4;                if(vis[3]==1)                {                    vis[2]++;                    vis[1]+=5;                }                if(vis[3]==2)                {                    vis[2]+=3;                    vis[1]+=6;                }                if(vis[3]==3)                {                    vis[2]+=5;                    vis[1]+=7;                }            }            if(b!=0)            {                if(b>=vis[2])                {                    b-=vis[2];                     vis[2]=0;                }                else                {                    vis[2]-=b;                    vis[1]+=vis[2]*4;                    b=0;                }                if(b>0)                {                    sum+=(b+8)*4/36;         //把为类型2转化为1,因为类型为2占的体积为4,就要占一个6*6类型的箱子                    vis[1]+=(9-b%9)*4;       //看类型为2的剩下的体积装1*1的体积                }             }            if(a!=0)            {                if(a>=vis[1])      //如果a大于vis[1],看还剩几个没装                {                    a-=vis[1];                }                else                    a=0;            }            sum+=(a+35)/36;      //将剩下的装入箱子中            cout<<sum<<endl;    }    return 0;}

的;
0 0
原创粉丝点击