function[xmax,pmax]=knaps(cl,pl,m1)
%[xmax,pmax]=knaps(c1.pl,m1)
%c1 pl are raw vectorg
%ml is the 1imit
%启动定时器
tic;
%保存当前最优解
global xmax;
xmax=zeros(size(c1));
global d;
[d,xi]=sort([c1./pl]
d=1./d;
global c;
c=c1(xi);
global P;
p=pl(xi);
global pmax;
pmax=O;
global m;
m=ml;
t=search(xmax,0);
[temp,i]=sort(xi);
xmax=xmax(i);
%输出结果
disp(['time consuming:'hum2str(toc)'s' 7]);
disp(['maximum price:'num2str(pmax)]);
disp(['total weight:' num2sir(xmax*cl')]);
disp{['indication of goods:'int2str(x=xmax)]);
return
%递归函数
function y=search(x,i)
global c;
global p;
global m;
global d;
global pmax;
global xmax;
%判断结果
if i>0,
temp=x;
temp(i)=1;
dc=m-temp*c';
if dc<0,
if x*p'>pmax,
pmax=x*p';
xmax=x;
y=1;
return
end
y=1;
return
elseif dc*d(i)+x+p' return; end if i==size(C,2), x(i)=1; if x*p'>pmax, pmax=x*p'; xmax=x; end Y=1; return end x(i)=1; end %新的递归 for j=i+1:Size(c,2), w=search(x,j); if w==1; break end end y=1; return 因篇幅问题不能全部显示,请点此查看更多更全内容