Detailed full array recursion (C ++)

A: full array Detailed recursive C ++

You are good! I was just entering the community of white!
If explanation is not in place, you also want high ape wing!

Two: introduced

First, I introduce nothing to do with this article, you can skip, chiefs hope the game development community to look at:

  1. I am faced with the pressure to find a job for the 2020 graduates are looking for a (unity or H5) the game client development positions. (Primary or intern)
  2. I am in this job for technology level considered moderate, the company hopes to have accepted, I am willing to pay for the company's own strength, progress together in herein.
  3. For related post, please click on send mail to my QQ mailbox ([email protected])

Three: Code

#include "pch.h"
#include<iostream>
using namespace std;
int a = 0;
//交换
void swap(int &a, int &b)
{
	int temp;
	temp = a;
	a = b;
	b = temp;
}
//全排列递归算法
void Perm(int list[], int k, int m)
{
	//list 数组存放排列的数,K表示层 代表第几个数,m表示数组的长度
	if (k == m)
	{
		//K==m 表示到达最后一个数,不能再交换,最终的排列的数需要输出;
		for (int i = 0; i <= m; i++)
			cout << list[i];
		cout << endl;
	}
	else {
		for (int i = k; i <= m; i++)
		{
			swap(list[i], list[k]);
			Perm(list, k + 1, m);
			swap(list[i], list[k]);
		}
	}
}
int main(void)
{
	int a[] = { 1,2,3,4 };
	int m = 2;
	Perm(a, 0, m);
	/*123,132,213,231,321,312*/
}

IV: Code detailed illustration

Here Insert Picture Description

Five: summary

I used recursive has been half-comprehended, by drawing a diagram, the results suddenly realized;
recommended for beginners and more graphically, multi-use pen and paper analyzed;
study hard, I hope we make progress together.

Published an original article · won praise 2 · views 46

Guess you like

Origin blog.csdn.net/Dream_TP/article/details/105374441