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

来源:互联网 发布:什么是大数据交易平台 编辑:程序博客网 时间:2024/06/04 20:06
#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;
}
原创粉丝点击