一.代码实现  

 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. UUIDGenerator

    import java.util.UUID; //下面就是实现为数据库获取一个唯一的主键id的代码 public class UUIDGenerator { public UUIDGenerator( ...

  2. flink ---- 系统内部消息传递的exactly once语义

    At Most once,At Least once和Exactly once 在分布式系统中,组成系统的各个计算机是独立的.这些计算机有可能fail. 一个sender发送一条message到rec ...

  3. ShenZhenXiaoLengHuanYou Technology Co.,Ltd 技术支持网站

    本网页为ShenZhenXiaoLengHuanYou Technology Co.,Ltd 团队的技术支持网址,如果在我们开发的游戏中遇到任何问题,欢迎联系我们! QQ:2535510006 邮箱: ...

  4. kubernetes 1.9 安装部署

    参考地址:https://github.com/gjmzj/kubeasz 引言 提供快速部署高可用k8s集群的工具,基于二进制方式部署和利用ansible-playbook实现自动化,既提供一键安装 ...

  5. [ARM-Linux开发] 主设备号--驱动模块与设备节点联系的纽带

    一.如何对设备操作 linux中对设备进行操作是通过文件的方式进行的,包括open.read.write.对于设备文件,一般称其为设备节点,节点有一个属性是设备号(主设备号.次设备号),其中主设备号将 ...

  6. Postgres-XL集群ERROR :Failed to get pooled connections原因说明

    集群说明 6台服务器.其中1台(rt67-1)运行GTM,其余5台均运行1个GTM_PROXY.1个Coordinator node.3个Data node.每个服务器连接到3组网络中,每个Data ...

  7. 006 SpringCloud 学习笔记2-----SpringCloud基础入门

    1.SpringCloud概述 微服务是一种架构方式,最终肯定需要技术架构去实施. 微服务的实现方式很多,但是最火的莫过于Spring Cloud了.SpringCloud优点: - 后台硬:作为Sp ...

  8. 004 Thymeleaf学习笔记

    1.Thymeleaf概述 SpringBoot并不推荐使用jsp,但是支持一些模板引擎技术:Freemarker.Thymeleaf.Mustache. 简单说, Thymeleaf 是一个跟 Ve ...

  9. [转帖]UML类图新手入门级介绍

    UML类图新手入门级介绍 2010-11-12 19:45:00 monkey_d_meng 阅读数 27230  收藏 文章标签: umlinterfaceclass编程扩展更多 分类专栏: 软件工 ...

  10. Mysql批量更新的三种方式

    前言 批量插入由于mysql的VALUES原生支持,使用较为便利. 批量更新的写法一般有三种,在更新数量较少的情况下,前两种性能不相上下.但是在更新字段增加,更新条数较多(500以上)建议使用第三种写 ...