列表

详情


SH7. 包裹运输

描述

工厂生产的产品包装在相同高度h,尺寸为1 * 1,2 * 2,3 * 3,4 * 4,5 * 5,6 * 6的方形包装中。 这些产品始终以与产品高度相同的尺寸为6 * 6的包裹交付给客户。因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的包裹数量。他们很需要有一个好的程序帮他们解决这个问题从而节省费用。现在这个程序由你来设计。

输入描述

输入文件包括几行,每一行代表一个订单。每个订单里的一行包括六个整数,中间用空格隔开,分别为 1*1 至 6*6 这六种产品的数量。输入文件将以 6 个 0 组成的一行结尾。

输出描述

除了输入的最后一行 6 个 0 以外,输入文件里每一行对应着输出文件的一行,每一行输出一个整数代表对应的订单所需的最小包裹数。

示例1

输入:

0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0

输出:

2
1

原站题解

C 解法, 执行用时: 2ms, 内存消耗: 224KB, 提交时间: 2020-12-09

#include <stdio.h>
int main()
{
    int a[7];
    int flag=1;
    while(flag)
    {
        flag=0;
        for(int i=1;i<7;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]!=0)
            {
               flag=1;
            }

        }
        int sum=0;
        sum=sum+a[6]+a[5]+a[4];
        if(11*a[5]>=a[1])
            a[1]=0;
        else
         a[1]=a[1]-11*a[5];
        if(5*a[4]<a[2])
          a[2]=a[2]-5*a[4];
        else
        {
            a[2]=0;
            int s=a[4]*5-a[2];
            if(s>=a[1])
            a[1]=0;
        else
          a[1]=a[1]-s;
        }
        int n=((9*a[3]+4*a[2]+a[1])/36);
        if(((9*a[3]+4*a[2]+a[1])%36)==0)
        sum=sum+n;
        else 
        sum=sum+n+1;
        if(flag)
        printf("%d\n",sum);
        

    }

}

C 解法, 执行用时: 2ms, 内存消耗: 228KB, 提交时间: 2020-04-04

#include<stdio.h>
int main(){
    int p[6];
    while(scanf("%d %d %d %d %d %d",&p[0],&p[1],&p[2],&p[3],&p[4],&p[5])==6){
        int count=0,temp1,temp2;
        if(p[5]>0){
            count+=p[5];}
        if(p[4]>0){
            count+=p[4];
            temp1=11*p[4];
            if(p[0]>temp1)
                p[0]-=temp1;
            else p[0]=0;
        }
        if(p[3]>0){
            count+=p[3];
            temp2=p[3]*5;
            if(p[1]>=temp2){
                p[1]-=temp2;}
            else {
                p[1]=0;
                temp1=(temp2-p[1])*4;
            if(p[0]>temp1)
                p[0]-=temp1;
            else p[0]=0;
            }
        }
        if(p[2]>0){
            count+=p[2]/4;
            if(p[2]%4>0){
                count++;
                temp2=(4-p[2]%4)*2-1;
                if(p[1]>=temp2){
                p[1]-=temp2;}
            else {
                p[1]=0;
                temp1=(temp2-p[1])*4+p[2]%4+4;
            if(p[0]>temp1)
                p[0]-=temp1;
            else p[0]=0;
            }
            }
        }
        temp1=p[0]/4;
        if(p[0]%4>0)
            temp1++;
        temp2=p[1]+temp1;
        if(temp2%9==0)
            count=count+temp2/9;
        else count=count+temp2/9+1;
        if(count>0)
            printf("%d\n",count);
    }
    return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 256KB, 提交时间: 2019-12-11

#include<stdio.h>
int main(){
    int nums[6];
    while(scanf("%d %d %d %d %d %d", &nums[0],&nums[1],&nums[2],&nums[3],&nums[4],&nums[5])==6){
        int area[2] = {0};  // 1*1 and 2*2
        int count = 0;
        if(nums[5] > 0){ // 6*6
            count += nums[5];
        }
        if(nums[4] > 0){ // 5*5
            count += nums[4];
            area[0] += nums[4]*11;
        }
        if(nums[3] > 0){ // 4*4
            count += nums[3];
            area[1] += nums[3]*5;
        }
        if(nums[2] > 0){ // 3*3
            count += nums[2] / 4;
            if(nums[2]%4!=0){
                count += 1;
                int left = 4 - (nums[2] - nums[2]/4 * 4);
                if(left==1){
                    area[0]+=5, area[1]+=1;
                }else if(left==2){
                    area[0]+=6, area[1]+=3;
                }else if(left==3){
                    area[0]+=7, area[1]+=5;
                }
            }
        }
        if(nums[1] > 0){ // 2*2
            if(area[1] > nums[1]){
                area[1] -= nums[1];
                area[0] += (area[1]*4);
            }else{
                nums[1] -= area[1];
                count += (nums[1]/9);
                if(nums[1]%9!=0){
                    count += 1;
                    int left = 9 - (nums[1] - nums[1]/9 * 9);
                    area[0] += left*4;
                }
            }
        }
        if(nums[0] > 0){ // 1*1
            if(area[0] < nums[0]){
                nums[0] -= area[0];
                count += (nums[0]/36);
                if(nums[0]%36!=0){
                    count += 1;
                }
            }
        }
        if(count>0)
            printf("%d\n", count);
    }
}

C++ 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2020-10-31

#include<cstdio>
#include<cstring>
int dir[4]={0,5,3,1};
int a[10];
int main()
{
	int i,sum,ans;
	while(1)
	{
		sum=0;
		for(i=1;i<7;++i)
		{
			scanf("%d",&a[i]);
			sum+=a[i];
		}
		if(!sum)
		break;
		ans=a[6]+a[5]+a[4]+(a[3]+3)/4;
		int cnt_2=a[4]*5+dir[a[3]%4];
		if(a[2]>cnt_2)
		ans+=(a[2]-cnt_2+8)/9;
		int cnt_1=ans*36-a[6]*36-a[5]*25-a[4]*16-a[3]*9-a[2]*4;
		if(a[1]>cnt_1)
		ans+=(a[1]-cnt_1+35)/36;
		printf("%d\n",ans);
	}
	return 0;
}

C 解法, 执行用时: 2ms, 内存消耗: 356KB, 提交时间: 2020-07-14

#include <stdio.h>
#include <stdlib.h>

int main(void) {
	int a[6],b[6];
	int i,flag=1,res,remain1=0,remain2=0,tmp;
	while(1)
	{
		res=0;
		flag=0;
		for(i=0;i<6;i++)
		{
			scanf("%d",&a[i]);
			if(a[i]!=0)
				flag=1;
		}
		if(flag==0)
			break;
		b[5]=a[5];
		b[4]=a[4];
		b[3]=a[3];
		res=b[3]+b[4]+b[5];
		remain1=b[3]*20-b[4]*11;
		remain2=b[3]*5;
		if(a[2]%4==0)
		{

			b[2]=a[2]/4;
			res+=b[2];
//			if(a[1]>a[3]*5)
//			{
//				b[1]=(a[1]-a[3]*5)/9;
//				res+=b[1];
//			}
		}
		else
		{
			tmp=a[2]%4;
			b[2]=a[2]/4+1;
			res+=b[2];
			remain1+=((4-tmp)*9);
			if(tmp==1)
			{
				remain2+=5;
			}
			else if(tmp==2)
			{
				remain2+=3;
			}
			else if(tmp==3)
			{
				remain2+=1;
			}
//			if(a[1]>(a[3]+1)*5)
//			{
//				b[1]=(a[1]-(a[3]+1)*5)/9;
//				res+=b[1];
//			}
		}
		if(a[1]>remain2)
		{
			res+=((a[1]-remain2)/9);
			remain1-=remain2*4;
		}
		else
		{
			remain1-=a[1]*4;
		}
		if(a[0]>remain1)
		{
			res+=(a[0]-remain1)/36;
		}
		printf("%d\n",res);
//		if(a[0]>(b[4]*11+b[3]*20+))
	}
}

上一题