d_perm.h

来自「这是数据结构和算法的国外经典书籍.清华大学出版社出版的<数据结构C++语言」· C头文件 代码 · 共 42 行

H
42
字号
#ifndef GENERATE_PERMUTATIONS
#define GENERATE_PERMUTATIONS

#include <vector>

#include "d_util.h"			// for writeVector()

using namespace std;

// output the list of all permutations of the
// integer values in the range [index,permList.size())
void permute(vector<int> permList, int index);

void permute(vector<int> permList, int index)
{
	int temp, i, vSize = permList.size();

	if (index == vSize-1)
		// display the permutation
		writeVector(permList);	
	else
	{
		// find all permutations over the range
		// [index, vSize)
		permute(permList, index+1);

		// exchange permList[index] with permList[i]
		// for i=index+1 to the end of the vector and
		// find all permutations
		for (i=index+1; i < vSize; i++)
		{
			temp = permList[i];							
			permList[i] = permList[index];
			permList[index] = temp;

			permute(permList, index+1);		
		}
	}
}

#endif	// GENERATE_PERMUTATIONS

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?