算法 递归与全排列

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
int data[]={
    
    1,2,3,4,5,6,7,8,9,10,11,12};
int num=0;
void perm(int begin,int end){
    
    
	if(begin==end){
    
    //取10个数中的m个数全排列 则begin==m 结束终止 
		/*for(int j=0;j<10;j++)cout<<data[j]<<" ";
				cout<<endl;*/
		num++;
	}
	else {
    
    
		for(int i=begin;i<=end;i++){
    
    
			swap(data[begin],data[i]);//把当前第1个数与后面的数交换位置 
			perm(begin+1,end);
			swap(data[begin],data[i]);//恢复,用于下次交换 
		}
	}
}
int main(){
    
    
	 perm(0,9);//do {}while(next_permutation(data,data+4));
	cout<<num;//n! 
	return 0;
	
}


猜你喜欢

转载自blog.csdn.net/Minelois/article/details/113854897