/*
*
*从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();
}
深度排序对0-n进行全排序
最新推荐文章于 2024-08-08 07:46:12 发布