全排列打印C++

   这个东西,实在实在是说不好.在前一段的时候,考虑别的问题的时候想到这个问题.自己研究了下,又上网看了一下,总算是有了个结果.至于更深入的,也没有去考虑.这个死角,触到了就好.

  最近重点Win32API,算法方面内容不多.不过,过一阵应该会多起来吧.

  贴.

// wholePermutation.cpp -- 2011-07-16-13.43
#include "stdafx.h"
#include 

const int SIZE = 4 ;

//	Use pointer if you plan to change its value,
//	Otherwise, use reference.
void swap (char * const pI, char * const pJ) ;
void printSet (const char * const set, const int size) ;
void printWholePermutationSet (char * const set, const int startIndex, const int endIndex) ;

int _tmain(int argc, _TCHAR* argv[])
{
	char set[SIZE] = {'a', 'b', 'c', 'd'} ;
	printWholePermutationSet(set, 0, SIZE - 1) ;

	return 0 ;
}

void swap (char * const pI, char * const pJ)
{
	char temp = *pI ;
	*pI = *pJ ;
	*pJ = temp ;
}

void printSet (const char * const set, const int size)
{
	using std ::cout ;

	for (int i = 0; i < size; ++i)
		cout << set[i] << ' ' ;
}

void printWholePermutationSet (char * const set, const int startIndex, const int endIndex)
{
	if (startIndex != endIndex)
	{
		for (int i = startIndex; i <= endIndex; ++i)
		{
			swap(set + i, set + startIndex) ;
			printWholePermutationSet(set, startIndex + 1, endIndex) ;
			swap(set + i, set + startIndex) ;
		}
	}
	else
	{
		printSet(set, endIndex + 1) ;
		std ::cout << std ::endl ;
	}
}

你可能感兴趣的:(算法)