【问题标题】:All possible combinations of numbers from a survey调查中所有可能的数字组合
【发布时间】:2021-02-08 19:53:19
【问题描述】:

我一直在尝试为此生成 Java 代码,但我需要帮助。我有一个包含10 问题的调查,每个问题都可以用15 之间的数字来回答。我需要创建所有可能组合的列表。所有问题都是必答题。

有了这个,我有以下几点:

问题:10

答案:1,2,3,4,5

想要的输出是这样的:

1111111111
1111111112
1111111113
1111111114
1111111115
1111111121
1111111122
1111111123
1111111124
1111111125
1111111131
1111111132
1111111133
1111111134
1111111135
1111111141
1111111142
. .
5555555555

代码可以选择更改问题的数量:而不是固定为10,而是将其更改为612,以及答案的数量:而不是仅将1改为@ 987654331@,把1改成4

这是我写的代码:

try {
    PrintWriter pw = new PrintWriter(new FileWriter(
            "C:\Users\User\Desktop\Test\prueba.txt"));

    int numResultados = 20;
    int numPreguntas = 10;
    int respuestaMax = 5;
    int respuestaMin = 1;

    int[] input = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    int posicion = 0;

    for (int k = 0; k <= numResultados; k++) {
        for (int j = 0; j < respuestaMax; j++) {
            for (int i = 0; i < input.length; i++) {
                System.out.print(input[i]);
            }
            System.out.println();
            input[posicion]++;
        }
        input[posicion] = 1;
        posicion++;
    }
} catch (Exception e) {
    e.printStackTrace();
}

但是我收到的输出不是我期望的输出,我被卡住了,提取:

1111111111
2111111111
3111111111
4111111111
5111111111
1111111111
1211111111
1311111111
1411111111
1511111111
1111111111
1121111111
1131111111
1141111111
1151111111
1111111111
1112111111
1113111111
1114111111
1115111111

【问题讨论】:

    标签: java combinations


    【解决方案1】:

    它应该使用递归来实现,通过创建一个前缀并附加它直到所有位置都被填满:

    static void printNum(int[] digits, int len, String prefix) {
        if (len == 0) { // all places are filled, print the result
            System.out.println(prefix); 
            return;
        }
        for (int i = 0; i < digits.length; i++) {
            printNum(digits, len - 1, prefix + digits[i]);
        }
    }
    
    // test method: 4 questions with 3 possible answers
    public static void main(String args[]) {
        int questionNum = 4;
        int[] answers = {1, 2, 3};
        
        printNum(answers, questionNum, "");
    }
    

    输出:

    1111
    1112
    1113
    1121
    1122
    1123
    1131
    1132
    1133
    1211
    ...
    3321
    3322
    3323
    3331
    3332
    3333
    

    更新
    现有代码存在以下问题:

    1. input 数组应该从最后一个索引打印到 0(以索引的相反顺序)
    2. 没有实现“进位”到更重要的位置,仅将当前posicion重置为1,这会导致重复的1111..11posicion需要重置。
    3. k 应在打印完答案集后立即递增。

    这些问题可以通过以下方式解决:

    int numPreguntas = 4;
    int respuestaMax = 5;
    int respuestaMin = 1;
    int numResultados = 20;
    
    int[] input = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    int posicion = 0;
    
    for (int k = 0; k < numResultados; ) {
        for (int j = 1; j <= respuestaMax; j++) {
            for (int i = input.length - 1; i >= 0; i--) {
                System.out.print(input[i]);
            }
            System.out.println();
            input[posicion]++;
            k++;
        }
        do { // fix carry
            input[posicion] = 1;
            if (++posicion < input.length) // prevent ArrayOutOfBounds
                input[posicion] += 1;
            else break;
        }
        while (input[posicion] > respuestaMax);
        posicion = 0; // reset position
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用mapreduce 方法生成可能组合的二维数组。在这种情况下,可能的组合数为5<sup>10</sup> = 9765625

      int questions = 10;
      int[] answers = {1, 2, 3, 4, 5};
      
      int[][] combinations = IntStream.range(0, questions)
              // prepare 2d arrays of questions and answers
              .mapToObj(i -> Arrays.stream(answers)
                      .mapToObj(j -> new int[]{j})
                      // stream of 2d arrays
                      .toArray(int[][]::new))
              // intermediate output
              .peek(arr -> System.out.println(Arrays.deepToString(arr)))
              // reduce stream of 2d arrays to a single 2d array
              .reduce((arr1, arr2) -> Arrays.stream(arr1)
                      // combinations of inner arrays
                      .flatMap(inner1 -> Arrays.stream(arr2)
                              // merge two inner arrays into one
                              .map(inner2 -> Stream.of(inner1, inner2)
                                      .flatMapToInt(Arrays::stream)
                                      .toArray()))
                      // array of combinations
                      .toArray(int[][]::new))
              // otherwise an empty 2d array
              .orElse(new int[0][]);
      
      // final output
      System.out.println("Number of combinations: " + combinations.length);
      // first 50 combinations by columns
      int rows = 10, limit = 50;
      IntStream.range(0, rows).forEach(i -> System.out.println(
              IntStream.range(0, limit).filter(j -> j % rows == i)
                      .mapToObj(j -> Arrays.stream(combinations[j])
                              .mapToObj(String::valueOf)
                              .collect(Collectors.joining()))
                      .collect(Collectors.joining(" "))));
      

      中间输出:

      [[1], [2], [3], [4], [5]]
      [[1], [2], [3], [4], [5]]
      [[1], [2], [3], [4], [5]]
      [[1], [2], [3], [4], [5]]
      [[1], [2], [3], [4], [5]]
      [[1], [2], [3], [4], [5]]
      [[1], [2], [3], [4], [5]]
      [[1], [2], [3], [4], [5]]
      [[1], [2], [3], [4], [5]]
      [[1], [2], [3], [4], [5]]
      

      最终输出,按列排列的前 50 个组合:

      Number of combinations: 9765625
      1111111111 1111111131 1111111151 1111111221 1111111241
      1111111112 1111111132 1111111152 1111111222 1111111242
      1111111113 1111111133 1111111153 1111111223 1111111243
      1111111114 1111111134 1111111154 1111111224 1111111244
      1111111115 1111111135 1111111155 1111111225 1111111245
      1111111121 1111111141 1111111211 1111111231 1111111251
      1111111122 1111111142 1111111212 1111111232 1111111252
      1111111123 1111111143 1111111213 1111111233 1111111253
      1111111124 1111111144 1111111214 1111111234 1111111254
      1111111125 1111111145 1111111215 1111111235 1111111255
      

      另见:How do I generate combinations of two arrays?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-29
        • 2011-08-10
        • 1970-01-01
        • 2012-03-20
        • 1970-01-01
        相关资源
        最近更新 更多