(排列数)输入两个正整数n,m(1<n<20,1<m<n),在1~n中任取m个数,按字典序从小到大输出所有这样的排列。例如:
输入:3 2
输出:1 2
1 3
2 1
2 3
3 1
3 2
const
SIZE:=25;
var
used: array[1.. SIZE] of boolean;
data: array[1.. SIZE] or integer;
n,m,i,j,k : integer;
flag: boolean;
begin readln(n,m);
fillchar (used,sizeof(used), false);
for i:=1 to m do
begin data[i]:=i;
used[i]:= true;
end;
flag:= true;
While flag do
begin for i:= 1 to m-1 do write(data[i],’ ‘);
writeln(data[m]);
flag:= false ;
for i:=m downto 1 do
begin used[data[i]]:=flase ;
for j:= data[i]+1 to n do if used[j]= false then
begin used[j]:= true;
data[i]:= j ;
flag:= true;
Break;
end;
if flag then
begin for k:=i+1 to m do
for j:=1 to n do if used[j]= false then
begin data[k]:= j;
used[j]:= true;
Break;
end;
break ;
end;
end;
end;
end.