自己认为这就是permutation 的函数的内容:
对于全排序来讲{1,2,3,4}
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
1 4 2 3
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
2 4 1 3
3 2 1 4
3 2 4 1
3 1 2 4
3 1 4 2
3 4 1 2
3 4 2 1
4 2 3 1
4 2 1 3
4 3 2 1
4 3 1 2
4 1 3 2
4 1 2 3
总共产生24种排列
用分治和递归的思想来解释这种问题
对于两个数来讲 直接交换位置就会出现两种情况
对于三个数来讲 将其中1个数提到最前面参考两个数的排序
对于四个数来讲 讲其中1个数提到最前面参考三个数的排序
------------------------------等等
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int a[4]={1,2,3,4};
void Perm(int beg,int end)
{
if(beg==end){
int i;
for(i=0;i<4;i++)
printf("%d ",a[i]);
printf("\n");
}
for(int i=beg;i<=end;i++){
swap(a[i],a[beg]); //将每一个数提到最前面
Perm(beg+1,end);
swap(a[i],a[beg]); //复原
}
}
int main()
{
int beg,end;
while(scanf("%d%d",&beg,&end)!=EOF){ //输入0 3 就可产生全排序
Perm(beg,end);
}
}

本文介绍了一个使用递归和分治思想实现的全排列算法。通过将数组中的元素依次提到最前面,并递归地生成剩余部分的所有排列组合,最终输出所有可能的排列。此算法能够清晰地展示全排列的生成过程。

被折叠的 条评论
为什么被折叠?



