代码实现排列组合【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 ...
随机推荐
- springMVC 数据模型相关注解 可注释类型 ModelAttribute SessionAttributes InitBinder
ModelAttribute 参数/方法SessionAttributes 类InitBinder 方法
- FineReport简单部署
一.部署方式 1.官网发布包部署 2.自定义tomcat部署 二.发布包部署 1.下载一个发布包:https://www.finereport.com/product/download 解压后打开bi ...
- PHP7 引入的“??” 和“?:”的区别
<?php $array = [ 'a' => 1, 'b' => 2, 'c' => [], ]; $a = $array['c'] ?? 0; $b = $array['c ...
- 【链接】在线压缩JS文件
在线压缩JS文件: http://yui.2clics.net/ https://refresh-sf.com/
- 如何在Debian 9上安装和使用Docker
介绍 Docker是一个简化容器中应用程序进程管理过程的应用程序.容器允许您在资源隔离的进程中运行应用程序.它们与虚拟机类似,但容器更便携,更加资源友好,并且更依赖于主机操作系统. 在本教程中,您将在 ...
- Java的集合类之 map 接口用法
Map接口不是Collection接口的继承.而是从自己的用于维护键-值关联的接口层次结构入手.按定义,该接口描述了从不重复的键到值的映射. 我们可以把这个接口方法分成三组操作:改变.查询和提供可选视 ...
- 【剑指offer】数组在排序数组中出现的次数
题目描述 统计一个数字在排序数组中出现的次数. 分析:数组有序,采用二分查找无疑 两种方法,时间复杂度差不多,都是利用二分查找,不过统计k出现的次数有所不同而已 方法1:二分查找k,找到任意一个k的下 ...
- ES6常用的新特性
1.Let&const <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- Python 入门(1):hello world 到流程控制
1.hello world 在D:\python\目录下新建文件hello.txt,编写代码如下 print("hello world!") 修改后缀名为.py,执行hello.p ...
- Service must be explitict android 5.0问题
如果target到API 21,有一些注意的事项,以下是目前我发现的两个问题1. Service must be explitict,从Lollipop开始,service必须显性声明,解决方案:ht ...