Problem Description
在一批共n个集装箱要装上艘载重量为c的轮船,其中集装箱i的重量为wi。找出一种最优装载方案,将轮船尽可能装满,即在装载体积不受限制的情况下,将尽可能重的集装箱装上轮船。
Input
输入有多组数据,每组数据的第一行有两个正整数n和c。n是集装箱数,c是轮船的载重量。接下来的1行中有n个正整数,表示集装箱的重量。
Output
对于每组数据输出最大装载重量。
Sample Input
5 10
7 2 6 5 4
Sample Output
10
Author
代码:
#include<stdio.h>
int c[41],max,n,m;
void search(int j,int t)
{
if(t>m)return ;
if(t>max)max=t;
if(max==m)return ;
for(int i=j;i<n;i++)
{
search(i+1,t+c[i]);
}
}
int main()
{
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
max=0;
for(i=0;i<n;i++)
scanf("%d",&c[i]);
search(0,0);
printf("%d\n",max);
}
return 0;
}
最优装载方案的算法实现

本文介绍了一种解决装载问题的算法,目标是在有限的船只载重下,最大化装载集装箱的总重量。通过输入集装箱数量和船只载重,输出最大装载重量。
1310

被折叠的 条评论
为什么被折叠?



