我们为大家收集整理了关于java排列组合算法,以方便大家参考。有时候,我们不仅需要知道排列或组合的数量,而且需要知道有哪些排列或组合,并列举出所有的排列或组合,人工列举工作量大而且容易出错,那么,如何利用计算机帮忙列举出所有的这些排列或组合呢?
一.利用二进制状态法求排列组合,此种方法比较容易懂,但是运行效率不高,小数据排列组合可以使用复制代码 代码如下:
import java.util.Arrays;
//利用二进制算法进行全排列
//count1:170187
//count2:291656public class test {
public static void main(String[] args) {
long start=System.currentTimeMillis();
count2();
long end=System.currentTimeMillis();
System.out.println(end-start);
}
private static void count2(){
int[] num=new int []{1,2,3,4,5,6,7,8,9};
for(int i=1;i<Math.pow(9, 9);i++){
String str=Integer.toString(i,9);
int sz=str.length();
for(int j=0;j<9-sz;j++){
str="0"+str;
}
char[] temp=str.toCharArray();
Arrays.sort(temp);
String gl=new String(temp);
if(!gl.equals("012345678")){
continue;
}
来源: http://www.51edu.com/it/bckf/493395.html