输出所有可能的排列组合

#include <iostream>
using namespace std;
//Function prototype
void swap(int &,int &);
void cal(int *, int, int length);
int main()
{
   int a[] = {1,2,3,4,5,6};
   cal(a,0,5);
   return 0;
}
void swap(int &a,int &b)
{
   int temp;
   temp = a;
   a = b;
   b = temp;
}
void cal(int *a,int first,int length)
{
   if (first == length)
   {
      for (int i=0;i<length;i++)
      {
         cout << a[i] << "";
      }
      cout << endl;
   }
   else
   {
     for (int i =first;i<length;i++)
      {
       swap(a[first], a[i]);
       cal(a,first+1,length);
       swap(a[first],a[i]);
      }
   }
}
 

posted on 2019-10-20 20:20  华1  阅读(665)  评论(0)    收藏  举报