1 solutions

  • 0
    @ 2024-12-11 0:16:56

    C++ :

    #include <iostream>
    #include <iomanip>
    
    #include <string>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    using namespace std;
    
    int n,a[21],count,s[12];
    bool f[21];
    
    
    void jh(int i,int j)
    {
    	int temp;
    	temp=a[i];
    	a[i]=a[j];
    	a[j]=temp;
    }
    
    void cz(int i)
    {
    	int j,k,flat;
    	if (i>n)//&&
    	{
    //		flat=0;
    		count++;
    		for (k=1;k<n;k++)
    			printf("%5d",a[k]);
    //		printf("\n");
    		printf("%5d\n",a[k]);
    //	    for (k=1;k<=n;k++)
      //          if (a[k]!=n-k+1)
      //              flat=1;
      //      if (flat)
    //            printf("",a[k]);  	
    		return ;
    	}
    	for (j=1;j<=n;j++)
    	{
    		if (f[j])
    			continue ;
    		a[i]=j;
    		f[j]=1;
    		cz(i+1);
    		f[j]=0;
    	}
    }
    
    int main()
    {
    	int i,j;
    	while (scanf("%d",&n)!=EOF)
    	{
    		count=0;
    		memset(f,0,sizeof(bool));
    		cz(1);
    //		printf("%d\n",count);
    	}
    	return 0;
    }
    
    

    Pascal :

    var n,m,i:longint;
        a,b:array[1..1000] of longint;
    procedure dfs(t:longint);
    var i:longint;
    begin
      if t>n then
      begin
        for i:=1 to n do write(a[i]:5);
        writeln;
        exit;
      end;
      for i:=1 to n do
      begin
        if b[i]=0 then
        begin
          b[i]:=1;
          a[t]:=i;
          dfs(t+1);
          b[i]:=0;
        end;
      end;
    end;
    begin
      readln(n);
      for i:=1 to n do
        b[i]:=0;
      dfs(1);
    end.
    
    • 1

    Information

    ID
    680
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By