zoukankan      html  css  js  c++  java
  • 分治法实现1-N的数字按字典序全排列组合 Java语言

    package 分治法;
    
    
    import java.util.Arrays;
    /*
     * 将数字 1 - n进行全排列  按字典序从小到大输出
     * 如  1 - 3  
     * 	123 132 213 231 312 321
     */
    class GenerateP{
    	
    	private int n;  //  求 1-n所有数字的全排列
    	private final int maxn = 110;//最多可排列组合的长度  1-100
    	private boolean [] hashTable;
    	private int [] p;
    	
    	public GenerateP(int n) {
    		// TODO Auto-generated constructor stub
    		this.n = n;
    		hashTable = new boolean[maxn];
    		p = new int [maxn];
    		Arrays.fill(hashTable, false);
    		Arrays.fill(p, 0);
    	}
    	public void generatep(int index){
    		if(index == n + 1){
    			for(int i = 1; i <= n; i++){
    				System.out.print(p[i]);
    			}
    			System.out.println();
    			return;
    		}
    		
    		for(int x = 1; x <= n; x++){
    			if(hashTable[x] == false){
    				p[index] = x;
     				hashTable[x] = true;
    				generatep(index + 1);
    				hashTable[x] = false;
    			}
    		}
    	}
    }
    public class Main {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		GenerateP generateP = new GenerateP(3);
    		generateP.generatep(1);
    
    	}
    
    }
    

      

  • 相关阅读:
    1、一条sql查询语句的执行过程
    go 内存分配
    GO Json
    gorm CRUD:读写数据
    go 基于切片的队列实现
    go的错误处理
    grpc
    sqlalchemy 判断字段是否存在
    定时函数
    用Python获取Linux资源信息的三种方法
  • 原文地址:https://www.cnblogs.com/mlsq2015/p/5898919.html
Copyright © 2011-2022 走看看