博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分治法实现1-N的数字按字典序全排列组合 Java语言
阅读量:4683 次
发布时间:2019-06-09

本文共 983 字,大约阅读时间需要 3 分钟。

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);	}}

  

转载于:https://www.cnblogs.com/mlsq2015/p/5898919.html

你可能感兴趣的文章
题目1462:两船载物问题
查看>>
题目38:守形数
查看>>
题目37:还是A+B
查看>>
Struts2中Action配置的三种方式
查看>>
promise学习总结
查看>>
使用gdb调试
查看>>
【整理】Git 学习
查看>>
有关于数据库设计!
查看>>
Eclipse中SVN插件的安装和配置(离线安装)
查看>>
POJ 2378 Tree Cutting(树形DP,水)
查看>>
第二冲刺阶段个人博客5
查看>>
UVA 116 Unidirectional TSP (白书dp)
查看>>
第三方测速工具
查看>>
MySQL 网络访问连接
查看>>
在aws ec2上使用root用户登录
查看>>
数据访问 投票习题
查看>>
CIO知识储备
查看>>
cnblog!i'm coming!
查看>>
使用点符号代替溢出的文本
查看>>
Axios 中文说明
查看>>