现在的位置: 首页 > 综合 > 正文

数组的全排列

2013年12月15日 ⁄ 综合 ⁄ 共 366字 ⁄ 字号 评论关闭
#include <iostream>
using namespace std;

void PrintPermutation(int n, int *A, int cur)
{
	if(n == cur)
	{
		for(int i = 0; i < cur; i++)
		{
			printf("%d ", A[i]);
		}
		printf("\n");
	}
	else
	{
		bool ok = false;
		for(int i = 0; i < n; i++)
		{
			ok = true;
			for(int j = 0; j < cur; j++)
			{
				if(A[j] == i) ok = false;
			}
			if(ok == true)
			{
				A[cur] = i;
				PrintPermutation(n, A, cur+1);
			}
		}
	}
}

int main()
{
	int n = 5;
	int A[1000] = {0};
	PrintPermutation(n, A, 0);
	system("pause");
	return 0;
}

抱歉!评论已关闭.