全排列问题 递归算法

来源:互联网 发布:windows10优化设置 编辑:程序博客网 时间:2024/05/07 18:14

#include<iostream>

using namespace std;

void Perm(Type *list,int k,int m)

{

   if(k == m)

  {

    cout<<list[i];

  }

  cout<<endl;

   else

  {

     for(int i=k;i<=m;i++)

    {

      Swap(list[k],list[i]);

      Perm(list,k+1,m);

      Swap(list[k],list[i]);

    }

  }

}

template<class Type>

void Swap(Type &a,Type &b)

{

 int temp = a;

  a=b;

  b=temp;

}

void main()

{

  int number;

  cout<<"number:";

  cin>>number;

  int *list = new list [number];

  for(int i=0;i<number;i++)

  {

    list[i]=i+1;

  }

  Perm(list,0,number-1);

}

1 0
原创粉丝点击