什么是C ++中的递归置换? [算法和源代码]

This article will describe a quick and easy algorithm that gives the full permutation for a natural number. The algorithm will be implemented by C++.

A full permutation is list of all variation for given items (usually numbers). For example, the full permutation of 3 elements are:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Also Read: C program to find factorial of any number using recursion
Also Read: C++ program to enter a number and print it into words

As we can easily calculate, the total number of iterations is n! (factorial). How do we get this number? The most simple way is to think of the number of different items we can choose for each position. For example, for the first place, we have n choices, and we have n-1 choices for the second place, and etc. Thus we have n*(n-1)*(n-2)*…1 that gives a total number n!.

So how to emulate this for the whole process? First, we can define a recursive function that ends printing one solution by checking if we have reached the end. For each iteration, we can simply swap the current item with the rest of the items and try the next position recursive. As we use a global array variable nums to keep the items, we need to swap the items back after each recursion call. The source code is pretty straight forward. Open Visual Studio, choose the ‘Console Application’ from ‘C++’ language projects.

And paste the following source code into Visual Studio.
#include <iostream>
using namespace std;
// helper dynamic array
int *nums;
// exchange two integers
inline void swap(int &i, int &j)
{
        int t = i;
        i = j;
        j = t;
}
// full permutation
void
perm(int n, int i)
{
        if (i == n – 1) //
check if reach the end of iteration
        {
               // print out
the iteration
               for (int j =
0; j < n; j ++)
               {
                       cout
<< nums[j];
               }
               cout <<
endl;
        }
        else
        {
               for (int j =
i; j < n; j ++)
               {
                       swap(nums[i],
nums[j]); // swap
                       perm(n,
i + 1);         // recursive
                       swap(nums[i],
nums[j]); // swap it back
               }
        }
}
// main entry
int
main()
{
        int n;
        cin >> n;
        nums = new int[n]; //
declare a dynamic array
        for (int i = 0; i
< n; i ++)
        {
               // and fill it
with numbers as 1, 2, 3, … n
               nums[i] = i +
1;
        }
        perm(n, 0);
        cin >> n; //
pause
        return 0;
}
Press F5 to run the project, put a number, e.g. 4, and the program will give the full permutation of 4. The last cin >> n is the C++ easy way to pause the screen after the program terminates.
Author’s bio:
Zhihua Lai is now a Marie Curie Experienced Researcher at University of Sheffield. At his spare time, he writes blogs and programs just for fun. He is also an ACMer where he is keen on fast and efficient algorithms. His blog can be found at http://helloacm.com/

翻译自: https://www.thecrazyprogrammer.com/2013/10/recursive-permutation-in-c.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值