我的PKU ACM POJ 1017解答

[原创]我的PKU ACM POJ 1017解答

开始自己想了一个,不过情况分的太多了,后来参考了别人的代码,发现和精简,自己总结了下改成下面这个简单的了。

Source Code

Problem: 1017 User: absolute
Memory: 204K Time: 0MS
Language: C++ Result: Accepted
  • Source Code
    #include <stdio.h>
    
    void POJ1017();
    
    int main()
    
    {
    
        POJ1017();
    
        return 0;
    
    }
    
    void POJ1017()
    
    {
    
        int packs[6],sum;
    
        int i;
    
        while(1)
    
        {
    
            sum=0;
    
            for(i=0;i<6;i++)
    
            {
    
                scanf("%d",packs+i);
    
                sum+=packs[i];
    
            }
    
            if(sum==0)
    
                return;
    
            int parcels=0;
    
            //6*6 packs
    
            parcels += packs[5];
    
            //5*5 packs
    
            parcels += packs[4];
    
            //剩下的只能塞1*1的packs
    
            packs[0] = packs[0]>packs[4]*11?packs[0]-packs[4]*11:0;
    
            //4*4 packs
    
            parcels += packs[3];
    
            //剩下的只能塞1*1,2*2的packs,2*2first
    
            //每个塞过4*4的还能再塞2*2的5个,塞完2*2后空的再塞1*1
    
            int pack2left = packs[3]*5 - packs[1];
    
            if(pack2left<=0)
    
            {
    
                packs[1] -= packs[3]*5;
    
            }
    
            else
    
            {
    
                packs[1]=0;
    
                packs[0] = packs[0]>pack2left*4?packs[0]-pack2left*4:0;
    
            }
    
            //每个6*6可以刚好塞满4个3*3,剩下的需另开包裹
    
            //剩1个3*3,可以再塞2*2的5个,剩2个3*3,可以再塞3个2*2,剩3个3*3可以再塞1个2*2
    
            //即7-2*pack3left,剩下的再塞1*1
    
            parcels += (packs[2]+3)/4;
    
            int pack3left = packs[2]%4;
    
            if(pack3left>0)
    
            {
    
                if((packs[1]-7+2*pack3left)>0)
    
                {
    
                    packs[0] = packs[0]>36-9*pack3left-(7-2*pack3left)*4?packs[0]-(36-9*pack3left-(7-2*pack3left)):0;
    
                    packs[1] -= 7-2*pack3left;
    
                }
    
                else
    
                {
    
                    packs[0] = packs[0]>36-9*pack3left-packs[1]*4?packs[0]-(36-9*pack3left-packs[1]*4):0;
    
                    packs[1] = 0;
    
                }
    
            }
    
            parcels += (packs[1]+8)/9;
    
            packs[1] %= 9;
    
            if(packs[1]>0)
    
            {
    
                packs[0] = packs[0]>36-packs[1]*4?packs[0]-(36-packs[1]*4):0;
    
            }
    
            if(packs[0]>0)
    
            {
    
                parcels += (packs[0]+35)/36;
    
            }
    
            printf("%d\n",parcels);
    
        }
    
    }

你可能感兴趣的:(ACM)