【问题标题】:I want to make list of characters to display in threes我想列出要显示的字符列表
【发布时间】:2018-01-18 02:21:42
【问题描述】:

我想列出要显示的字符列表,并且每个字符的出现次数不得超过 3 次。例如,如果我有 ABCDEF,它们应该以这种格式显示,ABC,DCA,CDB,FAE,EDB,EF,F。 两者都在Java中它与我的研究有关。 与我目前看到的相比,我被扔到了很远的地方。 以下是我的尝试

【问题讨论】:

  • Java 或 JavaScript 都与我的研究相关 -- 首先,我们不会为人们的家庭作业编写代码。如果您想做某事,请先自己尝试,然后在此处询问特定问题。而且我不明白作业怎么可能与完全不同的 Java 和 JavaScript 有关。
  • 谢谢。它真的是我最需要的java。但是虽然我说两者都是因为我现在都在学习。虽然我不能尝试是因为我是一个新手,但希望像你们中的一个一样。
  • 导入 java.util.Scanner;公共类 TenK { public static void main(String args[]){ Scanner man = new Scanner(System.in); System.out.println("输入字符:"); char 拥抱=man.nextchar(); } }
  • 我敢肯定你已经被告知了这一点,但你正在尝试做的是所谓的字符串排列。谷歌'使用 Java 的字符串排列'。你会对出现的情况感到惊讶。您需要学习 Java 或 JavaScript(两种完全不同的语言)的第一件事是如何付出某种形式的努力。在尝试另一种之前,尝试至少在一定程度上掌握其中一种。祝你好运。

标签: javascript java


【解决方案1】:

我找到a sample,改一下来解决你的问题。

// Java program to print all combination of size r in an array of size n
import java.io.*;

class Permutation {

    /* arr[]  ---> Input Array
    data[] ---> Temporary array to store current combination
    start & end ---> Staring and Ending indexes in arr[]
    index  ---> Current index in data[]
    r ---> Size of a combination to be printed */
    static void combinationUtil(int arr[], int data[], int start,
                                int end, int index, int r)
    {
        // Current combination is ready to be printed, print it
        if (index == r)
        {
            for (int j=0; j<r; j++)
                System.out.print(data[j]+" ");
            System.out.println("");
            return;
        }

        // replace index with all possible elements. The condition
        // "end-i+1 >= r-index" makes sure that including one element
        // at index will make a combination with remaining elements
        // at remaining positions
        for (int i=start; i<=end && end-i+1 >= r-index; i++)
        {
            data[index] = arr[i];
            combinationUtil(arr, data, i+1, end, index+1, r);
        }
    }

    // The main function that prints all combinations of size r
    // in arr[] of size n. This function mainly uses combinationUtil()
    static void printCombination(int arr[], int n, int r)
    {
        // A temporary array to store all combination one by one
        int data[]=new int[r];

        // Print all combination using temprary array 'data[]'
        combinationUtil(arr, data, 0, n-1, 0, r);
    }

    /*Driver function to check for above function*/
    public static void main (String[] args) {
        int arr[] = {1, 2, 3, 4, 5};
        int r = 3;
        int n = arr.length;
        printCombination(arr, n, r);
    }
}

/* This code is contributed by Devesh Agrawal */

this one

// Java program to print all permutations of a
// given string.
public class Permutation
{
    public static void main(String[] args)
    {
        String str = "ABC";
        int n = str.length();
        Permutation permutation = new Permutation();
        permutation.permute(str, 0, n-1);
    }

    /**
     * permutation function
     * @param str string to calculate permutation for
     * @param l starting index
     * @param r end index
     */
    private void permute(String str, int l, int r)
    {
        if (l == r)
            System.out.println(str);
        else
        {
            for (int i = l; i <= r; i++)
            {
                str = swap(str,l,i);
                permute(str, l+1, r);
                str = swap(str,l,i);
            }
        }
    }

    /**
     * Swap Characters at position
     * @param a string value
     * @param i position 1
     * @param j position 2
     * @return swapped string
     */
    public String swap(String a, int i, int j)
    {
        char temp;
        char[] charArray = a.toCharArray();
        temp = charArray[i] ;
        charArray[i] = charArray[j];
        charArray[j] = temp;
        return String.valueOf(charArray);
    }

}

// This code is contributed by Mihir Joshi

【讨论】:

    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多