采用递归生成排列

2020-09-10 21:43:07 浏览数 (1)

话不多说,代码如下:

代码语言:javascript复制
#include<iostream>
using namespace std;

inline void Swap(int &a, int &b)
{
    int temp = a;
    a = b;;
    b = temp;
}

void Perm(int list[], int begin, int end)
{
    if (begin == end)
    {
        for (int i = 0; i <= end; i  )
        {
            cout << list[i];
        }
        cout << endl;
    }
    else
    {
        for (int i = begin; i <= end; i  )
        {
            Swap(list[begin], list[i]);
            Perm(list, begin   1, end);
            Swap(list[begin], list[i]);
        }
    }
}

int main()
{
    int arr[3] = { 1, 2, 3 };
    Perm(arr, 0, 2);
    return 0;
}

0 人点赞