当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++进阶与实例

递归算法的全排列

    #include <stdio.h>
    int a[100];
    static int c;
    static int counter;
    bool check(int n,int m,int k)
    {
            int i;
            for(i=n;i>k;i--)
                    if(m==a[i])return false;
            return true;
    }
    void quanpailie(int n,int k)
    {
            int i,j;
            if(k>=1)
            {
                    for(i=n;i>=1;i--)
                    {
                            if(check(n,i,k))
                            {
                                    a[k]=i;
                                    quanpailie(n,k-1);
                            }
                    }
            }
            else
            {
                    for(j=c;j>=1;j--)
                            printf(" %d",a[j]);
                    printf("\n");
                    counter++;
            }
    }

    main()
    {
            a[0]=9;
            c=5;
            counter=0;
            quanpailie(9,5);
            printf("%d",counter);
            return 0;
    }

相关内容
赞助商链接