排列组合

 组合算法

这个算法的思路其实已经有人已经提出来了,在这里,我再具体写出来。

思路:

假设对{1,2,3,4,5}5个数进行5选3的组合,那么就用“00000”表示这5个数,其中“0”表示未被选中,“1”表示被选中,假设选中的是{1,2,3},那么就是“11100”。

(1)首先选中5个数的前3个,即{1,2,3},那么就是“11100”

(2)然后对“11100”从左到有扫描,找到第一个“10”串,将其变为“01”,并将其他的两个“1”放到整个串的最左端,就成了“11010”。(这里因为在“11100”中,第一个和第二个“1”已经在最左端了,如果没有在最左端,则要放到最左端。)

(3)重复步骤(2),直到最左端的“1”处在m-n的位置(这里的m=5,n=3),即“00111”,算法结束。

例:

11100-->11010-->10110-->01110-->11001-->10101-->01101-->10011-->01011-->00111

总共10种组合方法。

源代码如下:


public class Combination {
 public void combination() {//5选3
  int[] array = {1, 1, 1, 0, 0, 0, 0, 0};
  int i = 0;
  boolean isfirst = false;
  int isFirst = 0; //判断找到的是否是第一个 “10”串,如果是,那么记录“10”串中“1”的位置
  boolean isLast = false; //判断最左端的“1”是否在m-n的位置,m=5,n=3
  int n = 3;
  int count = 0;
  
  while(false == isLast) {
   for(i = 0; i < array.length; ++i) {
    System.out.print(array[i] + " ");
    if(false == isfirst) {
     if((1 == array[i]) && (0 == array[i+1])) {
      isFirst = i; //找到第一个“10”串中“1”的位置
      isfirst = true;
     }
    } 
   } 
   ++count;
   System.out.println();
   
   //交换第一个“10”串,使其变成“01”
   int tmp = array[isFirst];
   array[isFirst] = array[isFirst+1];
   array[isFirst+1] = tmp;
   isFirst ++;
   
   //判断最左边的“1”所处的位置是否等于m-n
   for(i = 0; i < array.length; ++i) {
    if(1 == array[i]) {
     if((array.length - n) == i) {
      isLast = true;
      break;
     }
     else break;
    }
   }
   
   //如果最左边的“1”所处的位置不等于m-n,那么将其他的“1”全部放到最左端
   if(false == isLast) {
    for(i = 0; i < isFirst; ++i) {
     if(1 == array[i]) {
      array[i] = 0;
      int  j = 0;
      while(j < isFirst) {
       if(0 == array[j]) {
        array[j] = 1;
        break;
       }
       else {
        ++j;
       }
      }///~while
     }
    }///~for
   }   
   isfirst = false;
  }///~while
  
  //打印最后一次的结果
  ++count;
  for(i = 0; i < array.length; ++i)
   System.out.print(array[i] + " ");
  System.out.println();
  System.out.println("共有" + count + "种组合方法!"); 
 } 
 
 
 public static void main(String[] args) {
  PermutationCombination pc = new PermutationCombination();
  pc.combination();
  
 }
}

 

运行结果:

1 1 1 0 0
1 1 0 1 0
1 0 1 1 0
0 1 1 1 0
1 1 0 0 1
1 0 1 0 1
0 1 1 0 1
1 0 0 1 1
0 1 0 1 1
0 0 1 1 1
共有10种组合方法!

 

全排列算法

想象一下八皇后问题。8个数的全排列相当于每列只能放一个数,保证列不冲突即可,不需要考虑对角线是否冲突,那么这不就是简化版的八皇后问题吗?

 

源代码如下:

public class Premutation {
 int[] array;
 int count = 0;
 
 public Premutation(int num) {
  this.array = new int[num];
 }
 
 public boolean canPlace(int lines, int rows) {//判断当前位置是否能放,即判断是否列冲突
  for(int j = 0; j < lines; ++j) {
   if(rows == array[j]) {
    return false;
   }
  }
  return true;
  
 }
 
 public void premutation(int line) {
  for(int row = 0; row < array.length; ++row) {//找到当前行能放的第一个位置
   if(true == canPlace(line, row)) {
    array [line] = row;
    if(array.length - 1 == line) {//如果放的是最后一个,那么放完后就打印出结果
     ++count;
     print();
    }
    else {
     premutation(line+1);
    }
     
   }
  }
 }
 
 public void print() {
  for(int i = 0; i < array.length; ++i)
   System.out.print(array[i] + " ");
  System.out.println();
 }
 
 public void total() {
  System.out.println("共有" + count + "种全排列方法!"); 
 }

 

  public static void main(String[] args) {
  long start = System.currentTimeMillis();
  Premutation pt = new Premutation(7);
  pt.premutation(0);
  pt.total();
  long end = System.currentTimeMillis();
  System.out.println((end - start) + " millis");
  
 }
}

 

运行结果如下:

共有40320种全排列方法!
3062 millis

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值