采用递归生成排列

话不多说,代码如下:

#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;
}
发布了177 篇原创文章 · 获赞 6 · 访问量 6399

猜你喜欢

转载自blog.csdn.net/qq_45585519/article/details/104589738
今日推荐