[转载]数组的全排列问题

 申明:转自http://blog.csdn.net/wencheng2998/article/details/5971194
 1 #include <iostream> 
 2 using namespace std;
 3 int cnt = 0;//累计全排列的总数
 4 
 5 
 6 void swap(char *a, char *b)
 7 {
 8     int temp;
 9     temp = *a;
10     *a = *b;
11     *b = temp;
12 }
13 //k表示从下表为k的元素开始排列,排列的范围k到m
14 void perm(char list[], int k, int m)
15 {
16     int i;
17     if (k == m)
18     {
19         for (i = 0; i <= m; i++)
20         {
21             cout << " " << list[i];
22         }
23         cout << endl;//没输出一组排序,输出一个换行符
24         cnt++;
25     }
26     else
27     {
28         for (i = k; i <= m; i++)
29         {
30             swap(&list[k], &list[i]);
31             perm(list, k + 1, m);
32             swap(&list[k], &list[i]);
33         }
34     }
35 }
36 int main()
37 {
38     char list[] = "12345";
39     perm(list, 0, 4);
40     cout << "全排列的总数是:" <<cnt << endl;
41     return 0;
42 }

 

posted on 2016-09-30 19:40  IT小新手  阅读(295)  评论(0编辑  收藏  举报

导航