代码实现排列组合【Java】
一.代码实现
package zhen;
import java.util.Arrays;
public class Arrangement {
/**
* 计算阶乘数,即n! = n * (n-1) * ... * 2 * 1
*/
private static long factorial(int n) {
long sum = 1;
while( n > 0 ) {
sum = sum * n--;
}
return sum;
}
/**
* 排列计算公式A = n!/(n - m)!
*/
public static long arrangement(int m, int n) {
return m <= n ? factorial(n) / factorial(n - m) : 0;
}
/**
* 排列选择(从列表中选择n个排列)
* @param dataList 待选列表
* @param n 选择个数
*/
public static void arrangementSelect(String[] dataList, int n) {
System.out.println(String.format("A(%d, %d) = %d", dataList.length, n, arrangement(n, dataList.length)));
arrangementSelect(dataList, new String[n], 0);
}
/**
* 排列选择
* @param dataList 待选列表
* @param resultList 前面(resultIndex-1)个的排列结果
* @param resultIndex 选择索引,从0开始
*/
private static void arrangementSelect(String[] dataList, String[] resultList, int resultIndex) {
int resultLen = resultList.length;
if(resultIndex >= resultLen) { // 全部选择完时,输出排列结果
System.out.println(Arrays.asList(resultList));
return;
}
// 递归选择下一个
for(int i = 0; i < dataList.length; i++) {
// 判断待选项是否存在于排列结果中
boolean exists = false;
for (int j = 0; j < resultIndex; j++) {
if (dataList[i].equals(resultList[j])) {
exists = true;
break;
}
}
if (!exists) { // 排列结果不存在该项,才可选择
resultList[resultIndex] = dataList[i];
arrangementSelect(dataList, resultList, resultIndex + 1);
}
}
}
/**
* 组合计算公式C<sup>m</sup><sub>n</sub> = n! / (m! * (n - m)!)
* @param m
* @param n
* @return
*/
public static long combination(int m, int n) {
return m <= n ? factorial(n) / (factorial(m) * factorial((n - m))) : 0;
}
/**
* 组合选择(从列表中选择n个组合)
* @param dataList 待选列表
* @param n 选择个数
*/
public static void combinationSelect(String[] dataList, int n) {
System.out.println(String.format("C(%d, %d) = %d", dataList.length, n, combination(n, dataList.length)));
combinationSelect(dataList, 0, new String[n], 0);
}
/**
* 组合选择
* @param dataList 待选列表
* @param dataIndex 待选开始索引
* @param resultList 前面(resultIndex-1)个的组合结果
* @param resultIndex 选择索引,从0开始
*/
private static void combinationSelect(String[] dataList, int dataIndex, String[] resultList, int resultIndex) {
int resultLen = resultList.length;
int resultCount = resultIndex + 1;
if (resultCount > resultLen) { // 全部选择完时,输出组合结果
System.out.println(Arrays.asList(resultList));
return;
}
// 递归选择下一个
for (int i = dataIndex; i < dataList.length + resultCount - resultLen; i++) {
resultList[resultIndex] = dataList[i];
combinationSelect(dataList, i + 1, resultList, resultIndex + 1);
}
}
public static void main(String[] args) {
String[] array = new String[4];
array[0] = "SG614111010000000010001";
array[1] = "SG614111020000000020001";
array[2] = "SG614111030000000030001";
array[3] = "SG614111040000000040001";
/**
* 测试排列
*/
System.out.println("测试排列:");
arrangementSelect(array, array.length);
/**
* 测试组合
*/
System.out.println("测试组合:");
for(int i = 1; i <= array.length; i++){
combinationSelect(array, i);
}
}
}
二.结果
测试排列:
A(4, 4) = 24
[SG614111010000000010001, SG614111020000000020001, SG614111030000000030001, SG614111040000000040001]
[SG614111010000000010001, SG614111020000000020001, SG614111040000000040001, SG614111030000000030001]
[SG614111010000000010001, SG614111030000000030001, SG614111020000000020001, SG614111040000000040001]
[SG614111010000000010001, SG614111030000000030001, SG614111040000000040001, SG614111020000000020001]
[SG614111010000000010001, SG614111040000000040001, SG614111020000000020001, SG614111030000000030001]
[SG614111010000000010001, SG614111040000000040001, SG614111030000000030001, SG614111020000000020001]
[SG614111020000000020001, SG614111010000000010001, SG614111030000000030001, SG614111040000000040001]
[SG614111020000000020001, SG614111010000000010001, SG614111040000000040001, SG614111030000000030001]
[SG614111020000000020001, SG614111030000000030001, SG614111010000000010001, SG614111040000000040001]
[SG614111020000000020001, SG614111030000000030001, SG614111040000000040001, SG614111010000000010001]
[SG614111020000000020001, SG614111040000000040001, SG614111010000000010001, SG614111030000000030001]
[SG614111020000000020001, SG614111040000000040001, SG614111030000000030001, SG614111010000000010001]
[SG614111030000000030001, SG614111010000000010001, SG614111020000000020001, SG614111040000000040001]
[SG614111030000000030001, SG614111010000000010001, SG614111040000000040001, SG614111020000000020001]
[SG614111030000000030001, SG614111020000000020001, SG614111010000000010001, SG614111040000000040001]
[SG614111030000000030001, SG614111020000000020001, SG614111040000000040001, SG614111010000000010001]
[SG614111030000000030001, SG614111040000000040001, SG614111010000000010001, SG614111020000000020001]
[SG614111030000000030001, SG614111040000000040001, SG614111020000000020001, SG614111010000000010001]
[SG614111040000000040001, SG614111010000000010001, SG614111020000000020001, SG614111030000000030001]
[SG614111040000000040001, SG614111010000000010001, SG614111030000000030001, SG614111020000000020001]
[SG614111040000000040001, SG614111020000000020001, SG614111010000000010001, SG614111030000000030001]
[SG614111040000000040001, SG614111020000000020001, SG614111030000000030001, SG614111010000000010001]
[SG614111040000000040001, SG614111030000000030001, SG614111010000000010001, SG614111020000000020001]
[SG614111040000000040001, SG614111030000000030001, SG614111020000000020001, SG614111010000000010001]
测试组合:
C(4, 1) = 4
[SG614111010000000010001]
[SG614111020000000020001]
[SG614111030000000030001]
[SG614111040000000040001]
C(4, 2) = 6
[SG614111010000000010001, SG614111020000000020001]
[SG614111010000000010001, SG614111030000000030001]
[SG614111010000000010001, SG614111040000000040001]
[SG614111020000000020001, SG614111030000000030001]
[SG614111020000000020001, SG614111040000000040001]
[SG614111030000000030001, SG614111040000000040001]
C(4, 3) = 4
[SG614111010000000010001, SG614111020000000020001, SG614111030000000030001]
[SG614111010000000010001, SG614111020000000020001, SG614111040000000040001]
[SG614111010000000010001, SG614111030000000030001, SG614111040000000040001]
[SG614111020000000020001, SG614111030000000030001, SG614111040000000040001]
C(4, 4) = 1
[SG614111010000000010001, SG614111020000000020001, SG614111030000000030001, SG614111040000000040001]
代码实现排列组合【Java】的更多相关文章
- 用js实现排列组合
在leetcode上看到一个题,代码实现排列组合的. 记得大学上课时候,就用c写过,现在用js试试,顺便看看耗时. 先看看3的阶乘: function permute(temArr,testArr){ ...
- java实现排列组合(通俗易懂)
个人感觉这篇文章(原文地址见文章尾)写的排列组合问题,非常的好,而且是一步一步引出排列组合问题,我也是看了这篇文章,一步一步按照这个思路来,最后会了自己的一套排列组合 也因此在算法竞赛中,两次用到了, ...
- Java蓝桥杯——排列组合
排列组合介绍 排列,就是指从给定n个数的元素中取出指定m个数的元素,进行排序. 组合,则是指从给定n个数的元素中仅仅取出指定m个数的元素,不考虑排序. 全排列(permutation) 以数字为例,全 ...
- HDU 4045 Machine scheduling (组合数学-斯特林数,组合数学-排列组合)
Machine scheduling Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 数组排列组合问题——BACKTRACKING
BACKTRACKING backtracking(回溯法)是一类递归算法,通常用于解决某类问题:要求找出答案空间中符合某种特定要求的答案,比如eight queens puzzle(将国际象棋的八个 ...
- 给定数组a[1,2,3],用a里面的元素来生成一个长度为5的数组,打印出其排列组合
给定数组a[1,2,3],用a里面的元素来生成一个长度为5的数组,打印出其排列组合 ruby代码: def all_possible_arr arr, length = 5 ret = [] leng ...
- LeetCode 77 Combinations(排列组合)
题目链接:https://leetcode.com/problems/combinations/#/description Problem:给两个正数分别为n和k,求出从1,2.......n这 ...
- hdu 4451 Dressing 排列组合/水题
Dressing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- LeetCode OJ:Combinations (排列组合)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
随机推荐
- ES6 - 解构(数组和对象)
解构对象 /** * 解构:快捷,方便 * * 对象解构 */ { var expense = { type: "es6", amount: "45" }; / ...
- 荔枝派zero从焊接到跑起linux
步骤 焊flash芯片(如果大于16M,需要改烧录工具的源码) 焊引脚,为了串口看数据 焊接flash芯片,需要注意1号脚的位置,flash芯片在开发板背面,1号脚位置是靠近麦克风的那边 以下为编译相 ...
- Cassandra开发入门文档第四部分(集合类型、元组类型、时间序列、计数列)
Cassandra 提供了三种集合类型,分别是Set,List,MapSet: 非重复集,存储了一组类型相同的不重复元素,当被查询时会返回排好序的结果,但是内部构成是无序的值,应该是在查询时对结果进行 ...
- Cassandra开发入门文档第二部分(timeuuid类型、复合主键、静态字段详解)
timeuuid类型 timeuuid具有唯一索引和日期时间的综合特性,可以与日期和时间函数联合使用,常用的关联函数: dateOf() now() minTimeuuid() and maxTime ...
- python初级(302) 7 列表
一.列表的概念: 1.创建一个列表 friends = list() 2.列表可以包含的内容: m_list = [5, 7, 9, 20] letters = ['a', 'b', 'e'] 3.从 ...
- windows下tomcat日志的坑
最近在调试工程,想着在windows下多开窗口操作方便,结果发现日志竟然不全,百思不得其解.最后发现竟然是部署的坑. 之前的部署都是在bin目录下运行service.bat安装服务后了事,启停都通过t ...
- light4j一个轻量级的低延时、高吞吐量、内存占用量小的API平台
1.背景(abstract) 笔者算是一个极客类型的程序员了.喜欢探索一些程序内在的原理.稳定性.自动化运维.健壮性,很多时间也会 去对程序的内存使用率.cpu使用率锱铢必较.尽量克扣掉不必要的cpu ...
- [Golang] ETCD键值监听器
0x0 需求 我们所有的服务启动后都以lease形式注册入ETCD,现要把这些服务监控起来. 0x1 ETCD key监听器实现 可动态增删要监听的键值对 https://github.com/bai ...
- Appium脚本(5) 元素等待方法示例
思考 在自动化过程中,元素出现受网络环境,设备性能等多种因素影响.因此元素加载的时间可能不一致,从而会导致元素无法定位超时报错,但是实际上元素是正常加载了的,只是出现时间晚一点而已.那么如何解决这个问 ...
- MySQL开发技巧 第二禅(子查询中匹配两个值、解决同属性多值过滤的问题、计算累进税的问题)
https://blog.csdn.net/xiesq5112/article/details/52154169