递归全排列(C#):
static int[] xx=new int[4];
static void Main(string[] args)
{
printP(4, xx, 0);
}
static void printP(int n,int [] A,int cur)
{
int i, j;
if (cur == n)
{
for (i = 0; i < n; i++)
Console.Write(A[i]);
Console.WriteLine();
}
else
for (i = 1; i <= n; i++)
{
int ok = 1;
for (j = 0; j < cur; j++)
if (A[j] == i) ok = 0;
if (ok == 1) { A[cur] = i; printP(n, A, cur + 1); }
}
}
时间: 2024-10-23 20:16:14