The examples in this article share with you the specific code of the Java arrangement and composition algorithm for your reference. The specific content is as follows
package BeanUtil;import java.util.ArrayList;import java.util.List;import com.work.core.exception.OurException;/** * Statistics the combination of the most chances of occurrence of any three* * @author wangmingjie * @date 2009-1-1 01:22:19 pm */public class Copy_2_of_StatisAnyThree {// Combination algorithm// The idea of this program is to open an array, and its subscript represents 1 to m numbers. The value of the array element is 1 to indicate its subscript// The number represented is selected, and if it is 0, it is not selected. // First initialize, set the first n elements of the array to 1, indicating that the first combination is the first n number. // Then scan the "10" combination of the array element value from left to right, find the first "10" combination and turn it into // "01" combination, and move all "1" on its left to the left to the leftmost end of the array. // When the first "1" moves to the mn position of the array, that is, when all n "1s" move to the rightmost end, you have to // to the last combination. // For example, find the combination of 3 in 5: // 1 1 1 0 0 //1,2,3 // 1 1 0 1 0 //1,2,4 // 1 0 1 1 0 //1,3,4 // 0 1 1 1 0 //2,3,4 // 0 1 1 0 1 //1,2,5 // 1 0 1 0 1 //1,3,5 // 0 1 1 0 1 //2,3,5 // 0 1 1 0 1 //1,4,5 // 0 1 0 1 1 1 //2,4,5 // 0 0 1 1 1 //3,4,5 public static void main(String[] args) { Copy_2_of_StatisAnyThree s = new Copy_2_of_StatisAnyThree(); s.printAnyThree(); } /** * */ public void printAnyThree(){ int[] num = new int[]{1,2,3,4,5,6}; print(combine(num,3)); } /** * Select m numbers from n numbers* @param a * @param m * @return */ public List combine(int[] a,int m){ int n = a.length; if(m>n){ throw new OurException("Error! There are only "+n+" elements in array a. "+m+" greater than "+2+"!!!"); } List result = new ArrayList(); int[] bs = new int[n]; for(int i=0;i<n;i++){ bs[i]=0; } //Initialize for(int i=0;i<m;i++){ bs[i]=1; } boolean flag = true; boolean tempFlag = false; int pos = 0; int sum = 0; //First find the first 10 combination, then becomes 01, and at the same time move all 1s on the left to the leftmost side of the array do{ sum = 0; pos = 0; tempFlag = true; result.add(print(bs,a,m)); for(int i=0;i<n-1;i++){ if(bs[i]==1 && bs[i+1]==0 ){ bs[i]=0; bs[i+1]=1; pos = i; break; } } //Move all the 1 on the left to the leftmost side of the array for(int i=0;i<pos;i++){ if(bs[i]==1){ sum++; } } for(int i=0;i<pos;i++){ if(i<sum){ bs[i]=1; }else{ bs[i]=0; } } //Check whether all the 1s have moved to the rightmost side of the array for(int i= nm;i<n;i++){ if(bs[i]==0){ tempFlag = false; break; } } if(tempFlag==false){ flag = true; }else{ flag = false; } }while(flag); result.add(print(bs,a,m)); return result; } private int[] print(int[] bs,int[] a,int m){ int[] result = new int[m]; int pos= 0; for(int i=0;i<bs.length;i++){ if(bs[i]==1){ result[pos]=a[i]; pos++; } } return result ; } private void print(List l){ for(int i=0;i<l.size();i++){ int[] a = (int[])l.get(i); for(int j=0;j<a.length;j++){ System.out.print(a[j]+"/t"); } System.out.println(); } }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.