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);   //递归调用 
    			}
    		}
    	} 
    } 
    
  • 相关阅读:
    权限管理命令
    常用命令2
    常用命令1
    queue
    poj 3984
    L3-008 喊山 (30 分)
    常州大学新生寒假训练会试 I 合成反应
    dfs 的全排列
    poj 1154
    hdu 1241
  • 原文地址:https://www.cnblogs.com/jxgapyw/p/5868046.html
Copyright © 2011-2022 走看看