資源簡介
基于matlab的01背包源碼實現(xiàn),純手寫,僅供新手學習和參考...由于代碼比較簡單,所以沒有過多的注釋,大家可以根據(jù)網(wǎng)上的帖子理解01背包的動態(tài)規(guī)劃思想,再自己臨摹代碼進行學習
代碼片段和文件信息
function?Back01()
????captical?=?15;??%?背包容量
????index?=?(1:1:8);?%?商品index
????len?=?length(index)?+?1;
????weigth?=?[073451723];??%商品重量
????score?=?[046821355];??%商品價值
????for?i?=?1:length(index)
????????fprintf(“商品:“+?index(i)?+“-重量:“?+?weigth(i)?+?“-價值:“?+?score(i)?+“\n“);
????end
????%?DP
????optinmal??=?zeros(lencaptical);?%
????for?i?=?2:len
????????for?j?=?1:captical
????????????%??如果當前得重量小于容量????????????
????????????if(weigth(i)?????????????????tmp?=?j-weigth(i);
%?????????????????fprintf(“***************?\n“);
%?????????????????fprintf(“tmp:“?+?tmp?+?“i:“?+?i?+“j:“?+?j
評論
共有 條評論