装载问题
有一批集裝箱要裝上一艘載重量為c的輪船。其中集裝箱i的重量為Wi。最優(yōu)裝載問題要求確定在裝載體積不受限制的情況下,將盡可能重的集裝箱裝上輪船。
對于給定的n個集裝箱和輪船的載重量C,編程計算裝入最多時的集裝箱個數(shù)。
輸入由多組測試數(shù)據(jù)組成。
每組測試數(shù)據(jù)輸入的第1行中有2個正整數(shù)n和C。正整數(shù)n是集裝箱個數(shù);正整數(shù)C是輪船的載重量。接下來的一行中有n個整數(shù),分別表示n個集裝箱的重量,它們之間用空格分隔。
對應每組輸入,輸出最大載重量
Sample Input
5 10 7 2 6 5 4
Sample Output
10 這題我是用搜索來做的 這題就是在一些數(shù)字里面,選一些數(shù)相加,使它們的和最大又不能大于c 先排序,再從大到小來搜 var n,c,i,tao,t:longint; b,s:array[0..5000]of longint;procedure kp(l,r:longint); var i,j,mid:longint; beginif l>=r then exit;i:=l;j:=r;mid:=s[(l+r) div 2];repeatwhile s[i]>mid do inc(i);while s[j]<mid do dec(j);if i<=j thenbegins[0]:=s[i];s[i]:=s[j];s[j]:=s[0];inc(i);dec(j);end;until i>j;kp(l,j);kp(i,r); end; procedure search(dep,t:longint); beginif t<c then if t>tao then tao:=t;if t=c thenbeginwrite(c);halt;end;if (t>c)or(dep>n) then exit;if (t+s[dep])<=c then search(dep+1,t+s[dep]);search(dep+1,t); end; beginreadln(n,c);fillchar(b,sizeof(b),0);for i:=1 to n doread(s[i]);kp(1,n);tao:=0;search(1,0);write(tao); end.
轉載于:https://www.cnblogs.com/YYC-0304/p/9500244.html
總結
- 上一篇: 工作分配问题pascal程序
- 下一篇: 字符序列pascal程序