猿问

如何在java中打印出X个排列?

我编写的代码采用数组的元素并遍历数组以给出所有排列。但我需要它只显示一定数量的排列:


最终代码仅给出 9 个元素的 6 个排列(换句话说,打印总共 362880 个输出的前 60480 个排列)。为简单起见,我使用数组中的 4 个元素,并打印出所有 24 个排列。但我需要代码适用于任意数量的排列。例如,如果我需要它打印出 1-permutation,代码应该打印前 4 个排列 - ABCD、ABDC、ACBD 和 ACDB。我不确定如何解决这个问题。


public static void main(String[] args) {

    // TODO Auto-generated method stub



    String[] myArray = {"A","B","C", "D"};

    int size = myArray.length; 

    permutation(myArray, 0, size-1);


    // Calculate Permutations

    int n=size;

    int r=6; // subject to change

    int p = n - r;

    int total=1;

    int total2=1;

    int total3=0;


    for (int top=n; top>0; top--)

    {

        total *= top;

    }


    if ((n-r<0))

    {

     System.out.println("r value cannot be greater than array size");

     total3=0;

    }

    else 

    {

        for (int bot=1; bot<=p; bot++)

        {

            if (p==0) // should be -- else if (p==0) -- after correction

            {

                total2=1;

            }

            else

            {

                total2 *= bot;

            }

        }

        total3 = total/total2;

    }


    System.out.printf("%d permutations of %d elements = %d\n",r,n,total3);

    // end calculation


}

// end main


// print array

public static void prtArray(String[] myArray, int size)

{

    for(int i=0; i<size; i++)

    {

        System.out.printf("%s", myArray[i]);

    }

    System.out.println();

}


// swap elements    

public static void swap(String[] myArray, int i, int j) {

    String temp;

    temp = myArray[i];

    myArray[i]=myArray[j];

    myArray[j]=temp;

}


// permutation 

private static void permutation(String[] myArray, int b, int e)

{

    if (b == e)

        prtArray(myArray, e+1); // accounts for array of size 1

    else

    {

        for(int i = b; i <= e; i++)

        {


            swap(myArray, i, b);

            permutation(myArray, b+1, e);

            swap(myArray, i, b);


        }

    }

}

}


有只小跳蛙
浏览 144回答 1
1回答

慕标5832272

我假设您不希望元素在排列中重复。例如。如果输入阵列{1, 2, 3, 4},则对于长度3: 123,124等是有效的排列,但122还是111不是。为了避免挑选已经挑选的元素,我们需要visited在递归中传递一个数组。public class Main {&nbsp; &nbsp; // Maintain a global counter. After finding a permutation, increment this.&nbsp;&nbsp; &nbsp; private static int count = 0;&nbsp; &nbsp; // pos is the current index, and K is the length of permutation you want to print, and N is the number of permutation you want to print.&nbsp; &nbsp; private static void printPermutations(int[] arr, int[] visited, int pos, int K, int N, String str) {&nbsp; &nbsp; &nbsp; &nbsp; // We have already found N number of permutations. We don't need anymore. So just return.&nbsp; &nbsp; &nbsp; &nbsp; if (count == N) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (pos == K) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(str);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++; // we have found a valid permutation, increment counter.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Only recur if the ith element is not visited.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (visited[i] == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // mark ith element as visited.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visited[i] = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printPermutations(arr, visited, pos + 1, K, N, str + arr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // unmark ith element as visited.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visited[i] = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int[] arr = {1, 2, 3, 4};&nbsp; &nbsp; &nbsp; &nbsp; int[] visited = {0, 0, 0, 0}; // same as size of input array.&nbsp; &nbsp; &nbsp; &nbsp; count = 0; // make sure to reset this counter everytime you call printPermutations.&nbsp; &nbsp; &nbsp; &nbsp; // let's print first 4 permutations of length 3.&nbsp; &nbsp; &nbsp; &nbsp; printPermutations(arr, visited, 0, 3, 4, "");&nbsp; &nbsp; }}输出:对于 N = 4 和 K = 3(即长度为 3 的前 4 个排列):printPermutations(arr, visited, 0, 3, 4, "");123124132134对于 N = 4 和 K = 4(即长度为 4 的前 4 个排列):printPermutations(arr, visited, 0, 4, 4, "");1234124313241342
随时随地看视频慕课网APP

相关分类

Java
我要回答