#include<iostream>
using namespace std;
int count = 0;
typedef int Element;
void swap(Element arr[], int i, int j){
Element temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void allSort(Element arr[],int start,int end){
//递归终止条件
if(start == end){
count++;
//3数组的长度 输出数组
for(int i =0 ;i < 3; i++){
cout<<arr[i];
}
cout<<endl;
return ;
}
//数组arr从start到end的所有记录都可以出现在第一个位置
for(int i = start; i < end; i++){
swap(arr,i,start); //每次交换i和start位置的数
allSort(arr,start+1,end);
swap(arr,i,start); //复原交换的数组,防止对下一次交换产生影响
}
}
int main()
{
int arr[] = {1,2,3};
allSort(arr,0,3);
cout<<count;
}
全排列模板
最新推荐文章于 2020-09-28 14:49:49 发布