Full array java

definition:

From any of n different elements taken m (m≤n) elements, lined up in a certain order, called a permutation of m elements taken from n different elements. When m = n all the arrangement of the whole arrangement is called.

Formula: whole arrangement number f (n) = n (1 = 0 is defined!)!

For example: find the full array 1,2,3,4,5,6,7,8,9

Ideas:

  1. The number nine began the first number by one in the number of all the obtained exchange
    . 1 2. 5. 4. 3. 6. 7. 8. 9
    2 . 1 . 3. 6. 7. 8. 9. 4. 5
    . 3 2 . 1 . 4. 5. 6. 7. 8. 9
    . 4 2. 3 . 1 . 5 . 6. 7. 8. 9
    . 5 2. 4. 3 . 1 . 6. 7. 9. 8
    . 6 2. 5. 4. 3 . 1 . 7. 8. 9
    . 7 2. 6. 5. 4. 3 . 1 . 8. 9
    . 8 2. 3. 4. 5. 6. 7 . 1 . 9
    . 9 2. 6. 7. 8. 5. 4. 3 . 1
  2. Ignore properly arranged in the first column, each row of the remaining number of 8 and then the same operation, for example the first row, to give:
    2 . 3 8. 9. 7. 6. 5. 4
    . 3 2 . 4. 5. 6. 7. 9 8
    . 4 . 3 2 . 5. 6 . 7. 8. 9
    . 5 . 3. 4 2 . 6. 7. 8. 9
    . 6 . 3. 4. 5 2 . 7. 8. 9
    . 7 . 3. 4. 5. 6 2 . 8. 9
    . 8 . 3. 4. 5. 6. 7 2 . 9
    . 9 . 3. 4. 5. 6. 7. 8 2
  3. Ignore properly arranged in the first column, each row of the remaining number of 7 and then the same operation ...
  4. When the first and last number coincides with the number of operation ends.

Code

import java.util.Arrays;
public class QuanPaiLie{	
	public static void perm(int[] a,int begin,int end){ 
	    if(begin==end) {
	    	System.out.println(Arrays.toString(a));
	    }else {
	    	for(int i=begin;i<=end;i++) {
	    		int temp=a[begin];
	    		a[begin]=a[i];
	    		a[i]=temp;
	    		perm(a,begin+1,end);
	    		temp=a[begin];
	    		a[begin]=a[i];
	    		a[i]=temp;
	    	}
	    }
	}  
	public static void main(String args[]){
		int[] in={1,2,3,4,5,6,7,8,9};
		perm(in,0,in.length-1);
	}
}
Published 16 original articles · won praise 0 · Views 224

Guess you like

Origin blog.csdn.net/qq_44713502/article/details/103481246