POJ1017——Packets(贪心)

题目链接

       一道很简单贪心,结果被自己做蠢了。题目要求有6种不同规格的箱子,分别是1*1,2*2,3*3,4*4,5*5,6*6。要对这6种规格的箱子打包,但是包裹的规格只有6*6的。所以问你如何用尽量少的包裹打包,输出包裹数。我的做法就是老老实实的模拟,先装6*6的,每个包裹只能装一个6*6,再装5*5,一个包裹只能装一个5*5和11个1*1,再是4*4。。。就这样一直模拟下去,然后写了一大堆。


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>

using namespace std;

int a[10]={0};

int main()
{
    //freopen("in.in","r",stdin);
    //freopen("out.out","w",stdout);
    while(scanf("%d",&a[1])!=EOF)
    {
        for(int i=2;i<=6;i++)
            scanf("%d",&a[i]);
        int f=1;
        for(int i=1;i<=6;i++)
            if(a[i])
            {
                f=0;
                break;
            }
        if(f)break;
        int ans=a[6]+a[5]+a[4];

        if(11*a[5]<a[1])a[1]-=11*a[5];//补充装有长为5的包裹
        else a[1]=0;

        if(5*a[4]<a[2])a[2]-=5*a[4];//补充装有长为4的包裹
        else
        {
            int temp=5*a[4]-a[2];
            if(4*temp<a[1])a[1]-=4*temp;
            else a[1]=0;
            a[2]=0;
        }

        int temp=a[3]%4;//补充装有长为3的包裹
        ans+=a[3]/4;
        if(temp)
        {
            ans++;
            if(temp==1)
            {
                int ter=7;

                if(a[2]>=5)a[2]-=5;
                else
                {
                    ter+=(5-a[2])*4;
                    a[2]=0;
                }

                if(ter<=a[1])a[1]-=ter;
                else a[1]=0;
            }
            else if(temp==2)
            {
                int ter=6;

                if(a[2]>=3)a[2]-=3;
                else
                {
                    ter+=(3-a[2])*4;
                    a[2]=0;
                }

                if(ter<=a[1])a[1]-=ter;
                else a[1]=0;
            }else
            {
                int ter=5;

                if(a[2]>=1)a[2]-=1;
                else
                {
                    ter+=(1-a[2])*4;
                    a[2]=0;
                }

                if(ter<=a[1])a[1]-=ter;
                else a[1]=0;
            }
        }

        ans+=a[2]/9;//补充装有长为2的包裹
        temp=(9-a[2]%9)*4;
        if(temp==36)temp=0;
        if(temp)ans++;
        if(temp<a[1])a[1]-=temp;
        else a[1]=0;

        ans+=a[1]/36;//补充装有长为1的包裹
        if(a[1]%36)ans++;

        printf("%d\n",ans);
    }
    return 0;
}

然后看到另外一则博客写的,非常机智的用简短的代码就处理了问题。链接如下:

http://blog.sina.com.cn/s/blog_6e265bbe01012ewn.html

还有一则博客附赠了好多测试数据,链接如下:

http://blog.sina.com.cn/s/blog_8a15ad1501015shf.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值