生成1-n的全排列...

 1 #include "stdafx.h"
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 const int MaxNum=9;
 6 int iArr[MaxNum];//定义数组
 7 void print(int cur,int n);
 8 inline void Swap(int *a,int *b)//交换两个位置的值
 9 {
10     int temp;
11     temp=*a;
12     *a=*b;
13     *b=temp;
14 }
15 int _tmain(int argc, _TCHAR* argv[])
16 {
17      for(int i=0;i<MaxNum;i++)iArr[i]=i+1;//初始化
18      print(0,2);
19     return 0;
20 }
21 
22 void print(int cur,int n)
23 {
24     if(cur==n)
25     {
26         for(int i = 0;i<=n;i++)
27             cout<<iArr[i];
28          cout<<endl;
29     }
30 
31     for(int i=cur;i<=n;i++){//第一次是无效替换——Swap(&iArr[k],&iArr[k]);这是个技巧
32         Swap(&iArr[cur],&iArr[i]);
33         print(cur+1,n);
34         Swap(&iArr[i],&iArr[cur]);
35     }
36     
37 
38 }
原文地址:https://www.cnblogs.com/crazycodehzp/p/3522364.html