aboutsummaryrefslogtreecommitdiff
path: root/tests/upload/full_permutation.sy
blob: 96f701cb372b1bcfd9feab5e64afbc633e1f7182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void permutation(int k, int n, int a[])
{
    int i = 0;
    if (k == n - 1)
	{
		while(i<n) {
			putint( a[i]);
      putch(45);
            i = i + 1;
		}
		putch(10);
	}
	else
	{
        i = k;
		while(i<n) {
			int temp = a[k];
			a[k] = a[i];
			a[i] = temp;
			//交换后递归下一层  
			permutation(k + 1, n, a);
			//保证每一层递归后保持上一层的顺序  
			temp = a[k];
			a[k] = a[i];
			a[i] = temp;
            i = i + 1;
        }
	}
}

int main()

{
	int a[100];
	int n;
	int k;
    int i = 0;
    while (i < 100) {
        a[i] = i + 1;
        i = i + 1;
    }
    n = getint();
    permutation(0, n, a);
	return 0;
}