输出数组的全排列

今天参照网上写的输出数组的全组合

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
/*writed by telnetning on 5/13,2013
*perm writed to output all Combination of a array
*/
    
#include <stdio.h>
    
/*swap used to switch two numbers value*/
void swap(int *pi, int*pj)
{
    int temp;
    temp = *pi;
    *pi = *pj;
    *pj = temp;
}
    
/*create all combination*/
void perm(int i,int n,int list[])
{
    int j;
    if(i == n){
        printf("list:");
        for(j = 0; j <= n ;j++){
            printf("%d ", list[j]);
        }
        printf("\n");
    else {
        for( j = i; j <= n; j++){
            swap(&list[i],&list[j]);
            perm(i+1,n,list);
            swap(&list[j],&list[i]);
        }
    }
}
    
int main()
{
    int list[8] = {1,2,3,4,5,6,7,8};
    
    perm(0,7,list);
    
    return 0;
}

 

原文地址:https://www.cnblogs.com/telnetning/p/3084833.html