装载问题



#include<iostream>

using namespace std;

int c1,c2,n;
int w[20];//用来存集装箱重量的
int s[100]={0},cnt=0;//装答案的
int sum;//用来表示集装箱总重的
int weight;//即时计算集装箱重量和
 
void search(int);//递归搜索
void output();

int main()
{
 int i;
 while(1)
 {
  cin>>c1>>c2>>n;
  if(c1==0&&c2==0&&n==0)
  {
   break;
  }
  else
  {
   for(i=1;i<=n;i++)
   {
    cin>>w[i];
    sum=sum+w[i];
   }
   search(1);
   weight=0;
   sum=0;
   cnt++;
  }
 }
 output();
 return(0);
}

void search(int m)
{
 int i;
 if(m==n+1)
 {
  if(weight<=c1&&(sum-weight)<=c2)
  {
   s[cnt]=1; 
  }
 }
 else
 {
  for(i=0;i<=1;i++)
  {
   if(weight+i*w[m]<=c1)
   {
    weight=weight+i*w[m];
    search(m+1);
    weight=weight-i*w[m];
   }
  }
 }
}

void output()
{
 int i;
 for(i=0;i<=cnt-1;i++)
 {
  if(s[i]==1)
  {
   cout<<"Yes"<<endl;
  }
  else
  {
   cout<<"No"<<endl;
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值