一.代码实现  

 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. kotlin基础 尾递归

    尾调用的重要性在于它可以不在调用栈上面添加一个新的堆栈帧——而是更新它,如同迭代一般. 尾递归因而具有两个特征: 调用自身函数(Self-called): 计算仅占用常量栈空间(Stack Space ...

  2. 014-交互式Shell和shell脚本获取进程 pid

    Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运行进程 1.交互式 Bash Shell 获取进程 pid 在已知进程名(name)的前提下,交互式 ...

  3. LeetCode 108. Convert Sorted Array to Binary Search Tree (将有序数组转换成BST)

    108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...

  4. Centos7环境下部署搭建discuz论坛

    1.首先搭建lnmp环境 2.从官网复制git地址(https://gitee.com/ComsenzDiscuz/DiscuzX),在服务器上安装git命令 yum install git -y  ...

  5. 远程控制分析之VBS编码转换

    简介 分析这种VBS简单chr()函数编码的脚本技巧.只需要把vbs的execute()函数换成信息输出到控制台(dos窗口)函数就可以了. WSH.Echo "print your mes ...

  6. Elasticsearch在windows上安装与使用

    Elasticsearch简称ES. 是一个全文搜索服务器,也可作为NoSQL数据库,存储任意格式的文档和数据,也可做大数据的分析,是一个跨界开源产品. ES的特点: 全文搜索引擎 文档存储和查询 大 ...

  7. npm 创建vue项目(指定目录进行创建)

    1.先安装node,js和npm 检验mpm 和node的方式是 npm -v  / node -v 2.安装最新版本 npm install @vue/cli -g 意外安装老版本的是代码  npm ...

  8. [转帖]亚马逊发布自主64核心ARM处理器:单核性能远超铂金至强

    亚马逊发布自主64核心ARM处理器:单核性能远超铂金至强 https://news.mydrivers.com/1/660/660383.htm 不知道真假 看样子比华为的鲲鹏920 要牛B . 亚马 ...

  9. was unable to refresh its cache! status = Cannot execute request on any known server

    出现这种错误是因为: Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为. 在 yml中设置 eureka.client.register-with-eu ...

  10. DS 红黑树详解

    通过上篇博客知道,二叉搜索树的局限在于不能完成自平衡,从而导致不能一直保持高性能. AVL树则定义了平衡因子绝对值不能大于1,使二叉搜索树达到了严格的高度平衡. 还有一种能自我调整的二叉搜索树, 红黑 ...