[问题1]:考虑由1到n(n<=9)按递增顺序排成的序列12345…n,在他们之间加入加号、减号和空格,分别使它们作加法、减法和将数字合并。然后求出结果,看看你是否得到零。程序找出所有长度为n的结果为零的序列。
程序如下:
ar n:vord
a:array[1..9]of byte;
procedure init;
begin
write(‘enter n:’);
readln(n);
a[n]:=1;
end;
procedure print;
const s:array[1..3]of string=(‘+’,‘ ’,‘-’);
var i:word;
begin
for i:=1 to n-1 do
write(i,s[a[i]]);
writeln(n,’=0’);
end;
procedure judge;
var i,ch:integer;
result,term:longint;
begin
result:=0;
ch:=1;
term:=1;
for i:=1 to n do
case a[i] of
1,3:begin result:=result+(2-ch)*term;
ch:=a[i];
term:=i+1;
end;
2:term:=term*10+i+1;
end;
if result=0 then print;
end;
procedure find(k:word);
var i:word;
begin
if k=n then judge
else
for i:=1 to 3 do
begin
a[k]:=i
find(k+1);
end;
end;
begin
init;
find(1);
end.