您的位置首页  科技

01背包问题 read(goods[i

programbeibao;constm=150;n=7;varxu:integer;i,j:integer;goods:array[1..n,0..2]ofinteger;ok:array[1..n,1..2]ofreal;procedureinit;vari:integer;beginxu:=m;fo

program beibao;constm=150;n=7;varxu:integer;i,j:integer;goods:array[1..n,0..2] of integer;ok:array[1..n,1..2] of real;procedure init;vari:integer;beginxu:=m;for i:=1 to n dobeginwrite('Enter the price and weight of the ',i,'th goods:');goods[i,0]:=i;read(goods[i,1],goods[i,2]);readln;ok[i,1]:=0; ok[i,2]:=0;end;end;procedure make;varbi:array[1..n] of real;i,j:integer;temp1,temp2,temp0:integer;beginfor i:=1 to n dobi[i]:=goods[i,1]/goods[i,2];for i:=1 to n-1 dofor j:=i+1 to n dobeginif bi[i]temp0:=goods[i,0]; temp1:=goods[i,1]; temp2:=goods[i,2];goods[i,0]:=goods[j,0]; goods[i,1]:=goods[j,1]; goods[i,2]:=goods[j,2];goods[j,0]:=temp0; goods[j,1]:=temp1; goods[j,2]:=temp2;end;end;end;begininit;make;for i:=1 to 7 dobeginif goods[i,2]>xu then break;ok[i,1]:=goods[i,0]; ok[i,2]:=1;xu:=xu-goods[i,2];end;j:=i;if i<=n thenbeginok[i,1]:=goods[i,0];ok[i,2]:=xu/goods[i,2];end;for i:=1 to j dowriteln(ok[i,1]:1:0,':',ok[i,2]*goods[i,2]:2:1);end.黄敏婷2021-03-15 07:16:28用贪心做!01背包用DP

免责声明:本站所有信息均搜集自互联网,并不代表本站观点,本站不对其真实合法性负责。如有信息侵犯了您的权益,请告知,本站将立刻处理。
网站推荐更多>>