亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用公式 N^R 的所有組合填充數組

用公式 N^R 的所有組合填充數組

守著星空守著你 2022-10-20 15:19:44
對于一個家庭作業問題,我需要用公式的所有組合填充一個數組N^R。變量R是常數并且是6。變量N不是常數,假設它是2. 所以2^6 = 64?,F在我需要的是一個包含所有組合的數組(在這種情況下64)。我找到了一個完全符合我需要的網站,在這種情況下的輸出應該是:[0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 1],[0, 0, 0, 0, 1, 0],[0, 0, 0, 0, 1, 1],[0, 0, 0, 1, 0, 0],[0, 0, 0, 1, 0, 1],[0, 0, 0, 1, 1, 0],[0, 0, 0, 1, 1, 1],[0, 0, 1, 0, 0, 0],[0, 0, 1, 0, 0, 1],[0, 0, 1, 0, 1, 0],[0, 0, 1, 0, 1, 1],[0, 0, 1, 1, 0, 0],[0, 0, 1, 1, 0, 1],[0, 0, 1, 1, 1, 0],[0, 0, 1, 1, 1, 1],[0, 1, 0, 0, 0, 0],[0, 1, 0, 0, 0, 1],[0, 1, 0, 0, 1, 0],[0, 1, 0, 0, 1, 1],[0, 1, 0, 1, 0, 0],[0, 1, 0, 1, 0, 1],[0, 1, 0, 1, 1, 0],[0, 1, 0, 1, 1, 1],[0, 1, 1, 0, 0, 0],[0, 1, 1, 0, 0, 1],[0, 1, 1, 0, 1, 0],[0, 1, 1, 0, 1, 1],[0, 1, 1, 1, 0, 0],[0, 1, 1, 1, 0, 1],[0, 1, 1, 1, 1, 0],[0, 1, 1, 1, 1, 1],[1, 0, 0, 0, 0, 0],[1, 0, 0, 0, 0, 1],[1, 0, 0, 0, 1, 0],[1, 0, 0, 0, 1, 1],[1, 0, 0, 1, 0, 0],[1, 0, 0, 1, 0, 1],[1, 0, 0, 1, 1, 0],[1, 0, 0, 1, 1, 1],[1, 0, 1, 0, 0, 0],[1, 0, 1, 0, 0, 1],[1, 0, 1, 0, 1, 0],[1, 0, 1, 0, 1, 1],[1, 0, 1, 1, 0, 0],[1, 0, 1, 1, 0, 1],[1, 0, 1, 1, 1, 0],[1, 0, 1, 1, 1, 1],[1, 1, 0, 0, 0, 0],[1, 1, 0, 0, 0, 1],[1, 1, 0, 0, 1, 0],[1, 1, 0, 0, 1, 1],[1, 1, 0, 1, 0, 0],[1, 1, 0, 1, 0, 1],[1, 1, 0, 1, 1, 0],[1, 1, 0, 1, 1, 1],[1, 1, 1, 0, 0, 0],[1, 1, 1, 0, 0, 1],[1, 1, 1, 0, 1, 0],[1, 1, 1, 0, 1, 1],[1, 1, 1, 1, 0, 0],[1, 1, 1, 1, 0, 1],[1, 1, 1, 1, 1, 0],[1, 1, 1, 1, 1, 1]我嘗試使用 for 循環實現這一點,但沒有成功。我不想要使這成為可能的算法的完整代碼,但我希望在路上得到幫助。提前致謝。
查看完整描述

1 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

我想出了這個解決方案,這有點笨拙,但可能適合你的情況,評論應該解釋一切:


public static void printCombinations(int R, int N) {

    // calculate the combinations

    String[][] combinations = calculateCombinations(R, N);

    // iterate over all

    for (int i = 0; i < combinations.length; i++) {

        // prints the commas at the end

        if (i != 0) {

            System.out.println(',');

        }

        // print to std out

        System.out.print(Arrays.toString(combinations[i]));

    }

    System.out.println();

}


public static String[][] calculateCombinations(int R, int N) {

    // calculate our limit

    int limit = (int) StrictMath.pow(N, R);

    // create the result array

    String[][] result = new String[limit][R];

    // iterate over all possibilities

    for (int i = 0; i < limit; i++) {

        // convert to base

        String base = Long.toString(i, N);

        // holds our temporary value

        StringBuilder intermediate = new StringBuilder(R);

        // pad the value from the start with zeroes if needed

        for (int sub = R - base.length(); sub > 0; sub--) {

            intermediate.append('0');

        }

        // append our number

        intermediate.append(base);


        // append to result

        result[i] = intermediate.toString().split("");

    }

    // return the result

    return result;

}

然后可以這樣調用以漂亮地打印出來:


printCombinations(6, 2);

或者得到它的結果:


String[][] result = calculateCombinations(6, 2);

運行演示


查看完整回答
反對 回復 2022-10-20
  • 1 回答
  • 0 關注
  • 94 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號