算法设计与分析之不定期更新的日常3+贪心
3.装箱问题
代码实现:
#include <iostream>
#include <cstdio>///贪心算法求解装箱问题,分类讨论
using namespace std;
int countain1[4]={0,7,6,5};
int countain2[4]={0,5,3,1};
int nTotal;
int main()
{
int b1,b2,b3,b4,b5,b6;
int c1,c2;
while(scanf("%d%d%d%d%d%d",&b1,&b2,&b3,&b4,&b5,&b6)!=EOF)
{
if(b1==0&&b2==0&&b3==0&&b4==0&&b5==0&&b6==0)
break;
nTotal=b6+b5+b4+(b3+3)/4;
c1=c2=0;
c1=b5*11;
c1+=countain1[b3%4];
c2=b4*5;
c2+=countain2[b3%4];
if(b2<=c2)
{
c1+=(c2-b2)*4;
}
else
{
nTotal+=(b2-c2)/9;
int r2=(b2-c2)%9;
if(r2)
{
nTotal++;
c1+=36-r2*4;
}
}
if(b1>c1)
{
nTotal+=(b1-c1+35)/36;
}
printf("%d\n",nTotal);
}
return 0;
}
/*
1 2 3 4 5 6
*/