首页 > 学院 > 开发设计 > 正文

全排列问题 递归算法

2019-11-11 05:09:22
字体:
来源:转载
供稿:网友

#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);

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表