zoukankan      html  css  js  c++  java
  • 全排列(按字典序)

    
    输出1到n的全排列 (字典序) 
    
    伪代码:
    void print_per(序列A, 集合S){
    	if(S为空){
    		输出序列A 
    	}else{
    		按照从小到大的顺序考虑S中的每个元素V
    		print_per(在A的末尾添加V得到的新序列, S-{V}); 
    	}
    } 
    
    void print_per(int n, int cur, int* a){
    	if(n == cur)   //递归边界 
    	    for(i=0; i<n; i++)
    	        cout << a[i] << " ";
    	else{
    		for(i=1; i<=n; i++){    //尝试在a[cur]中填各种整数i 
    			bool ok = true;
    			for(j=0; j<cur && ok; j++)
    			    if(i == a[j])     //i已经在a[0] 到 a[cur]中出现过,则不能再选 
    			        ok = false;
    			if(ok){     
    				a[cur] = i;
    				print_per(n, cur+1, a);   //递归调用 
    			}
    		}
    	} 
    } 
    
  • 相关阅读:
    3.27上午
    3.24上午 补
    2017.3.27下午
    2017.3.27上午
    2017.3.24下午
    2017.3.24上午
    2017.3.23下午
    2017.3.23上午
    2017.3.22上午
    2017.3.21下午
  • 原文地址:https://www.cnblogs.com/jxgapyw/p/5868046.html
Copyright © 2011-2022 走看看