如何将排列结果存储在2D数组中?

时间:2014-12-26 01:00:16

标签: arrays algorithm permutation

我需要帮助编写此代码才能获得数字的排列。

我需要将所有排列存储在2D数组中。

在输出排列之后,我需要在一种方法中处理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);
  }
 
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

这是我的解决方案而不是2d数组使用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();


    }    
}

然后你所要做的就是将列表发送到其他功能,只使用你需要的功能。