深度排序對0-n進行全排序

/*
*
*從0-n的整數的全排序
*使用深度搜索
*/
#include"stdio.h"
int n;
int a[10];
int book[10];
int num_sort;
//深度搜索
void dfs(int step)
{
	int i;
	if(step==n+1)
	{
		for(i=1;i<=n;i++)
		 	printf("%d",a[i]);
		printf("\n");
		num_sort++;
	}
	for(i=0;i<=n;i++){
		if(book[i]==0)
		{
			a[step]=i;
			if(a[1]==0)
				continue;
			book[i]=1;
			dfs(step+1);
			book[i]=0;
		}
	}
}
void main()
{
	printf("深度搜索進行全排序\n");
	printf("輸入排序數字的個數\n");
	scanf("%d",&n);
	printf("全排序爲:\n");
	dfs(1);
	printf("1-%d的全排序的個數爲%d",n,num_sort);
	getchar();
		getchar();
	
}

發佈了38 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章