Java版排列组合工具类 - Java Permutation and Combination Tools


( All code listed in this article is included in my personal lib, and the repo is hosted at: https://github.com/raistlic/LibRaistCommon )


最近在整理个人代码,有些觉得可能有用的,拿出来共享一下 大笑


先上用法示例代码:


问题一: 有三个字符串 "a", "b", "c",进行排列,列出共有多少种排列方式

public class PNCDemo {
  
  public static void main(String[] args) {
    
    System.out.println("===== demo permutation :");
    for(List<String> list : Permutation.of(Arrays.asList("a", "b", "c")))
      System.out.println(list);
  }
}
运行效果:

run:
===== demo permutation :
[a, b, c]
[a, c, b]
[b, a, c]
[b, c, a]
[c, a, b]
[c, b, a]
BUILD SUCCESSFUL (total time: 0 seconds)


问题二: 从五个数 1, 2, 3, 4, 5 中任取 3 个数,列出共有多少种取法

public class PNCDemo {
  
  public static void main(String[] args) {
    
    System.out.println("===== demo combination :");
    for(List<Integer> list : Combination.of(Arrays.asList(1, 2, 3, 4, 5), 3))
      System.out.println(list);
  }
}
运行效果:
run:
===== demo combination :
[1, 2, 3]
[1, 2, 4]
[1, 2, 5]
[1, 3, 4]
[1, 3, 5]
[1, 4, 5]
[2, 3, 4]
[2, 3, 5]
[2, 4, 5]
[3, 4, 5]
BUILD SUCCESSFUL (total time: 0 seconds)


问题三: 从五个数 1, 2, 3, 4, 5 中任取 3 个数进行排列,列出共有多少种排列方式

public class PNCDemo {
  
  public static void main(String[] args) {
    
    System.out.println("===== demo both :");
    for(List<Integer> list : Permutation.of(Arrays.asList(1, 2, 3, 4, 5), 3))
      System.out.println(list);
  }
}
运行效果:

run:
===== demo both :
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
[1, 2, 4]
[1, 4, 2]
[2, 1, 4]
[2, 4, 1]
[4, 1, 2]
[4, 2, 1]
[1, 2, 5]
[1, 5, 2]
[2, 1, 5]
[2, 5, 1]
[5, 1, 2]
[5, 2, 1]
[1, 3, 4]
[1, 4, 3]
[3, 1, 4]
[3, 4, 1]
[4, 1, 3]
[4, 3, 1]
[1, 3, 5]
[1, 5, 3]
[3, 1, 5]
[3, 5, 1]
[5, 1, 3]
[5, 3, 1]
[1, 4, 5]
[1, 5, 4]
[4, 1, 5]
[4, 5, 1]
[5, 1, 4]
[5, 4, 1]
[2, 3, 4]
[2, 4, 3]
[3, 2, 4]
[3, 4, 2]
[4, 2, 3]
[4, 3, 2]
[2, 3, 5]
[2, 5, 3]
[3, 2, 5]
[3, 5, 2]
[5, 2, 3]
[5, 3, 2]
[2, 4, 5]
[2, 5, 4]
[4, 2, 5]
[4, 5, 2]
[5, 2, 4]
[5, 4, 2]
[3, 4, 5]
[3, 5, 4]
[4, 3, 5]
[4, 5, 3]
[5, 3, 4]
[5, 4, 3]
BUILD SUCCESSFUL (total time: 0 seconds)


话说算法这种东西,似乎天生适合写成“策略接口” ———— 夫算法者,策略者也。


第一个文件: 排列和组合都要用到的阶乘类(原类名没有改,去掉了与本题无关的部分)

/*
 * Copyright 2012 wuyou (raistlic@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.math.BigInteger;

/**
 * This class
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
package com.hexiang.utils.arrange; public class TryCombination{ public static void main(String[] args){ System.out.println("对整数数组进行组合:C(n,n)"); int[] intArray=new int[4]; for(int i=0;i<intArray.length;i++){ intArray[i]=i+1; } System.out.println("对整数数组进行组合:C(4,4)"); Combination intCombination1=new Combination(intArray.length); while(intCombination1.hasMore()){ int[] index=intCombination1.getNext(); for(int i=0;i<intArray.length;i++){ if(index[i]!=0){ System.out.print(intArray[index[i]*i]+" "); } } System.out.println(); } System.out.println("对整数数组进行组合:C(4,3)"); Combination intCombination2=new Combination(intArray.length,3); while(intCombination2.hasMore()){ int[] index=intCombination2.getNext(); for(int i=0;i<intArray.length;i++){ if(index[i]!=0){ System.out.print(intArray[index[i]*i]+" "); } } System.out.println(); } String str="abcd"; char[] chArray=str.toCharArray(); System.out.println("对字符数组进行组合:C(4,4)"); Combination strCombination1=new Combination(chArray.length); while(strCombination1.hasMore()){ int[] index=strCombination1.getNext(); for(int i=0;i<chArray.length;i++){ if(index[i]!=0){ System.out.print(chArray[index[i]*i]+" "); } } System.out.println(); } System.out.println("对字符数组进行组合:C(4,3)"); Combination strCombination2=new Combination(chArray.length,3); while(strCombination2.hasMore()){ int[] index=strCombination2.getNext(); for(int i=0;i<chArray.length;i++){ if(index[i]!=0){ System.out.print(chArray[index[i]*i]+" "); } } System.out.println(); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值