一.代码实现  

 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】的更多相关文章

  1. 用js实现排列组合

    在leetcode上看到一个题,代码实现排列组合的. 记得大学上课时候,就用c写过,现在用js试试,顺便看看耗时. 先看看3的阶乘: function permute(temArr,testArr){ ...

  2. java实现排列组合(通俗易懂)

    个人感觉这篇文章(原文地址见文章尾)写的排列组合问题,非常的好,而且是一步一步引出排列组合问题,我也是看了这篇文章,一步一步按照这个思路来,最后会了自己的一套排列组合 也因此在算法竞赛中,两次用到了, ...

  3. Java蓝桥杯——排列组合

    排列组合介绍 排列,就是指从给定n个数的元素中取出指定m个数的元素,进行排序. 组合,则是指从给定n个数的元素中仅仅取出指定m个数的元素,不考虑排序. 全排列(permutation) 以数字为例,全 ...

  4. HDU 4045 Machine scheduling (组合数学-斯特林数,组合数学-排列组合)

    Machine scheduling Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. 数组排列组合问题——BACKTRACKING

    BACKTRACKING backtracking(回溯法)是一类递归算法,通常用于解决某类问题:要求找出答案空间中符合某种特定要求的答案,比如eight queens puzzle(将国际象棋的八个 ...

  6. 给定数组a[1,2,3],用a里面的元素来生成一个长度为5的数组,打印出其排列组合

    给定数组a[1,2,3],用a里面的元素来生成一个长度为5的数组,打印出其排列组合 ruby代码: def all_possible_arr arr, length = 5 ret = [] leng ...

  7. LeetCode 77 Combinations(排列组合)

    题目链接:https://leetcode.com/problems/combinations/#/description    Problem:给两个正数分别为n和k,求出从1,2.......n这 ...

  8. hdu 4451 Dressing 排列组合/水题

    Dressing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  9. LeetCode OJ:Combinations (排列组合)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

随机推荐

  1. android mk 预编译库

    LOCAL_PATH := $(call my-dir) #include $(CLEAR_VARS) # OpenCV #OPENCV_CAMERA_MODULES:=on #OPENCV_INST ...

  2. wms证书异常问题

    目前我司已定位到两个原因,详细如下, 1.  快速生成的证书存在问题,导致APACHE和NGINX显示的时间都是4号凌晨 2.  贵司在配置完成162和163两台应用的APACHE证书,以及其中10. ...

  3. Jenkins build 后 tomcat 启不来

    Jenkins build 后 war 包复制到 tomcat 下,启不来 添加 :export BUILD_ID=dontKillMe /usr/local/iron/tomcat8085/bin/ ...

  4. springboot+mybatisplus+druid数据库

    1.添加maven依赖 <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis ...

  5. matlab学习笔记13_1 函数返回值

    一起来学matlab-matlab学习笔记13函数 13_1 函数返回值 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 https://blog.csdn.net/qq_36556 ...

  6. PMP ITTO工具

    整合管理 范围管理 进度管理 成本管理 质量 资源管理 沟通管理 风险管理 采购管理 相关方管理

  7. IIS配置实现反向代理(图文)

    需求: 网站在备案,本来的网站不符合要求,先反向到别的网站.原网站:test.com, 目标网站:target.com 设置反向代理的服务器一定是在原网站服务器上. 注意:iis应该是iis7及以上版 ...

  8. mysql语句笔记

    创建数据库 create database name(自定义): #创建一个数据库   name自己取 create database if not exists name() default cha ...

  9. 【LOJ2292】[THUSC2016]成绩单(区间DP)

    题目 LOJ2292 分析 比较神奇的一个区间 DP ,我看了很多题解都没看懂,大约是我比较菜罢. 先明确一下题意:abcde 取完 c 后变成 abde ,可以取 bd 这样取 c 后新增的连续段. ...

  10. Ubuntu 定时关机

    sudo shutdown +120 Shutdown scheduled for Sat 2019-09-21 23:48:44 CST, use 'shutdown -c' to cancel.