2014-12-26 43 views
0

我需要幫助編寫此代碼來獲取數字的排列。如何將排列結果存儲在二維數組中?

我需要將所有排列存儲在二維數組中。

輸出置換後,我需要在一種方法中處理30%的置換,而在另一種方法中處理剩餘的30%。

我的代碼:

public class Permutation { 
 
    
 
* @param args the command line arguments 
 
*/ 
 
        
 
    void printArray(int []a) { 
 
    for (int i = 0; i< a.length; i++) { 
 
     System.out.print(a[i]+" "); 
 
    } 
 

 
    System.out.println(""); 
 
    } 
 

 
    void permute(int []a,int k) { 
 
    if(k==a.length) 
 
     printArray(a); 
 
    else 
 
     for (int i = k; i< a.length; i++) { 
 
     int temp=a[k]; 
 
     a[k]=a[i]; 
 
     a[i]=temp; 
 
     permute(a,k+1); 
 
     temp=a[k]; 
 
     a[k]=a[i]; 
 
     a[i]=temp; 
 
     } 
 
    } 
 

 
    public static void main(String[] args) { 
 
    Permutation p=new Permutation(); 
 
    
 
    int a[]={1,2,3,4,5,6}; 
 
    p.permute(a, 2); 
 
    } 
 
    
 
}

回答

0

這是我的解決方案,而不是一個二維數組使用ArrayList

import java.util.ArrayList; 
import java.util.Arrays; 

/** 
* 
* @author David 
*/ 
public class Permutations { 
    public static int[] a; 
    public final int SIZE = 6; 
    public final int NUMPERM; 
    public final ArrayList<int[]> newlist; 

    public Permutations() 
    { 

     a = new int[SIZE]; 
     for(int x = 0; x < SIZE; x++) 
      a[x] = x+1; 
     NUMPERM = Factorial(a.length); 
     newlist = new ArrayList<>(NUMPERM);  
    } 
    public void permute() 
    { 
     permutation(a,0,a.length); 
    } 

    private void permutation(int array[],int start, int end) 
    { 
     newlist.add(saveArray(array));   

     if (start<end) 
     { 
      int i,j; 
      for(i=end-2; i>=start; i--) 
      { 
       for(j=i+1; j<end; j++) 
       { 
        Swap(array,i,j); 
        permutation(array,i+1,end); 
       } 
       Rotate_Left(array,i,end); 
      } 
     } 
    } 
    private int[] saveArray(int[] array) 
    { 
     int[] newarray = new int[array.length]; 

     System.arraycopy(array, 0, newarray, 0, array.length); 
     return newarray;  
    } 

    public void Print() 
    {  //just to prove the list works 
     System.out.println("the current size of newlist is : " + newlist.size()); 
     int[] array = new int[a.length];   
     for(int x = 0; x < newlist.size(); x++) 
     {    
      array = newlist.get(x); 
      System.out.println(Arrays.toString(array)); 
     } 
    } 
    private void Swap(int array[],int i,int j) 
    { 
     int t; 
     t = array[i]; 
     array[i] = array[j]; 
     array[j] = t; 
    } 
    private void Rotate_Left(int array[],int start,int end) 
    { 
     int tmp = array[start]; 
     for (int i=start; i < end-1; i++) 
     { 
      array[i] = array[i+1]; 
     } 
     array[end-1] = tmp; 
    } 
    private int Factorial(int a) 
    { 
     int fact = 1; 
     for(int x = a; x > 0; x++) 
      fact *= a; 
     return fact; 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     Permutations newperm = new Permutations(); 
     newperm.permute(); 
     newperm.Print(); 


    }  
} 

那麼所有你需要做的就是發送列表其他功能,只使用你需要的東西。