全排列acc pascal程序加题解

 
  

 
  

 
  

全排列acc pascal程序加题解

全排列

Time Limit:20000MS  Memory Limit:65536K Total Submit:506 Accepted:218

Description

列出所有数字1到数字n的连续自然数的排列,要求所产生的任一数字序列中不允许出现得复数字。

Input

输入:n(1<=n<=9)

Output

由1~n组成的所有不重复的数字序列,每行一个序列。

Sample Input

3

Sample Output

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Source

elba

程序如下
const
  maxn=1000;
var
  a:array[0..maxn] of longint;
  n,k:longint;
  x:array[0..maxn] of boolean;
procedure init;
begin
  read(n);
end;
procedure aa;//输出。
var
  i:integer;
begin
  for i:=1 to n do

    write(a[i],' ');
  writeln;
end;
procedure js;//搜索。
var i,j:integer;
begin
  if k>n then//步速
  begin
    aa;//符合就输出。
    exit;//结束程序。
  end;
  for i:=1 to n do
  begin
    if not x[i] then//判断是否用过。
    begin
      x[i]:=true;//x[i]设置为用过
      a[k]:=i;//放入输出数组。
      k:=k+1;//步数+1
      js;//回溯,步数-1.
      k:=k-1;//
回溯,步数-1.
x[i]:=false;//设为没用过。 end; end;end;begin init;//输入。 k:=1;//第一步 js;//搜索。end.


你可能感兴趣的:(pascal,杂乱题目,pascal,搜索)