您的位置:首页 > 理论基础 > 数据结构算法

全排列 数据结构(机工出版) 递归算法 有改动 还有不完善的地方!

2013-10-14 23:31 387 查看
#include<iostream>

using namespace std;

void swap(int *x,int *y)

{

    int temp=*x;

    *x=*y;

    *y=temp;

}

void perm(int *list,int i,int n)

{

    int j,temp;

    if(i==n)

    {

        for(j =0; j <= n; j++)

            cout<<list[j]<<" ";

        cout<<endl;

    }

    else

    {

        for(j=i;j<=n;j++)

        {

            swap(list[i],list[j]);

            perm(list,i+1,n);

            swap(list[i],list[j]);

        }

    }

}

int main()

{

    int a[1010],n;

    while(cin>>n)

    {

        for(int i=n;i<n;i++)

        {

            cin>>a[i];

        }

        perm(a,0,n-1);

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息