集合的全排列问题

 1 #include<iostream>
 2 using namespace std;
 3 int from,to;
 4 void perm(int list[],int k,int m)
 5 {
 6     int j;
 7     if(k==m)
 8     {
 9         for(j=from-1;j<=to-1;j++)
10             cout<<list[j]<<" ";
11         cout<<endl;
12     }
13     else
14     {
15         for(j=k;j<=m;j++)
16         {
17             swap(list[k],list[j]);
18             perm(list,k+1,m);
19             swap(list[j],list[k]);
20         }
21     }
22 }
23 int main()
24 {
25     int list[100];
26     int temp,n;;
27     cin>>n>>from>>to;
28     temp=n;
29     while(temp--)
30     {
31         cin>>list[n-temp-1];
32     }
33     perm(list,from-1,to-1);
34     return 0;
35 }

输入:

  首先,输入n,代表n个元素;然后,输入from,to,表示在n个元素中对第from个元素到第to个元素进行全排列;最后,输入n个元素。

输出:

  第from个元素到第to个元素的全排列。

eg.

输入:

    6
    2 4
    1 2 3 4 5 6

输出:
    2 3 4
    2 4 3
    3 2 4
    3 4 2
    4 3 2
    4 2 3

原文地址:https://www.cnblogs.com/xdbingo/p/4806352.html