procedure TForm1.Button1Click(Sender: TObject);
const m=7;t=15;
var w:array[0..m] of integer;function knap(s:integer;n:integer):boolean;
begin
if (s=0) then
begin
result:=true;
exit;
end;
if (s<0) or ((s>0) and (n<1)) then
begin
result:=false
end;
if knap(s-w[n],n-1)=1 then
begin
write(w[n]);result:=true;
end;
result:=knap(s,n-1)
end;
begin
w[0]:=0;w[1]:=1;w[2]:=4;w[3]:=3;w[4]:=4;
w[5]:=5;w[6]:=2;w[7]:=7;
if (knap(t,m)) then writeln('ok')
else writeln('no!')
end;
const m=7;t=15;
var w:array[0..m] of integer;function knap(s:integer;n:integer):boolean;
begin
if (s=0) then
begin
result:=true;
exit;
end;
if (s<0) or ((s>0) and (n<1)) then
begin
result:=false
end;
if knap(s-w[n],n-1)=1 then
begin
write(w[n]);result:=true;
end;
result:=knap(s,n-1)
end;
begin
w[0]:=0;w[1]:=1;w[2]:=4;w[3]:=3;w[4]:=4;
w[5]:=5;w[6]:=2;w[7]:=7;
if (knap(t,m)) then writeln('ok')
else writeln('no!')
end;
解决方案 »
- 真假?Delphi for iPhone 1.0 released!
- 恭喜二位新版主上任,散分
- Delphi7检测到错误类 EAccessViolation,错误信息:'Access violation at address 0047D5C7 in module
- 国内仿Excel最完美的电子表格控件
- missing connection or connectionstring是什么问题啊
- 懵懂菜鸟发问:如何在已有的component基础上增加属性和方法?
- 困惑的查询问题,帮忙哦!
- 如何用SQL语句查询多张表的不同字段?
- 如何在winNT、win2000启动时运行指定程序?
- 搞不懂,是Delphi真的可以这样用,还是别的什么??谁帮我解释一下,谢谢了。
- 调用CHM格式的帮助文件
- ■■■ .cmb 是什么文件格式? 用什么软件可以打开? ■■■
else writeln('no!')
顯示出來?
writenln在windows方式下沒甚麼用吧
用showmessage('ok')好了
方法是:File-->New-->Other-->new-->Console Application
求pascal的解法.谢谢
c语言的程序是这个
#include<stdio.h>
#define N 7
#define S 15
int w[N+1] = {0,1,4,3,4,5,2,7};
int knap ( int S, int n)
{ if(S == 0) return 1; if ( s<O || ( s>O && n<1 )) return 0; if (knap(s-w[n],n-1)) { printf( "4d",w[n] );return 1; } return knap(s,n-1);
}
main() { if ( knap(S,N) ) printf( "OK!\n" ); else printf( "N0!\n" );
}