第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Java:從輸入字符串?dāng)?shù)組生成 nCr 數(shù)組并返回它

Java:從輸入字符串?dāng)?shù)組生成 nCr 數(shù)組并返回它

函數(shù)式編程 2024-01-05 09:54:54
我想從輸入數(shù)組返回所有可能組合的完整數(shù)組。我想生成 n 選擇 k 組合,其中 k=1 到 n。到目前為止我一直非常不成功。static void combinationUtil(String[] arr, String data[], int start, int end, int index, int r, float[][] info) {        // Current combination is ready to be printed, print it        strat newStrat = new strat(0, 0, 0, null);        if (index == r) {            //THIS IS WHERE THE COMBINATION I WANT APPEARS            return;        }        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, info);        }        return;    }public static void getCombinations(String[] arr, int n, int r, float[][] info) {        String[] data = new String[r];        combinationUtil(arr, data, 0, n - 1, 0, r, info);    }public static void main(String[] args) throws IOException, InterruptedException {        //Array I want to get all k 1:n combinations of        String[] array = { "TST1", "TST2", "TST3"}         //start a timer because that's always fun        long startTime = System.nanoTime();        //cycle through all 'pick k values'        for (int i = 1; i < 8; i++) {            getCombinations(array, n, i, info);        }        //Math's up. How Long did that take?        long endTime = System.nanoTime();        //IDEALLY PRINT THE COMBINATIONAL ARRAY HERE        System.out.println(Arrays.deepToString(_____));        //Don't forget to print the time ;)        System.out.println("Duration: "+(endTime - startTime)+" ns");    }我已經(jīng)嘗試了所有我能想到的方法和谷歌。從將“data”數(shù)組傳遞給函數(shù),將其與其先前的自身連接,將舊數(shù)組復(fù)制到新數(shù)組,其中最新索引是最新的“data”,ArrayLists,Stacks,.push(),.add() ,獲取可能組合的總數(shù)并將它們插入到全局?jǐn)?shù)組索引中...什么都沒有...我被燒毀了..當(dāng)然理想情況下結(jié)果將如下所示:[["TST1"], ["TST2"], ["TST3"], ["TST1", "TST2"], ["TST1", "TST3"], ["TST2", "TST3"], ["TST1", "TST2", "TST3"]此時(shí)甚至可以添加一點(diǎn)"It is done. Go. Be happy!"上面的代碼工作得很好,但組合只出現(xiàn)在combinationUtil()中,而不是我想在main()中使用累積結(jié)果的地方。那么,我到底做錯(cuò)了什么?
查看完整描述

1 回答

?
瀟瀟雨雨

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊

您想要計(jì)算大小為 n 的數(shù)組中 r 元素的可能組合。你可以試試這個(gè)代碼。我將該函數(shù)稱為 nCr(不確定這是否是我們要解決的問題的正確數(shù)學(xué)符號(hào))


public static void main(String[] args) {

    String[] array2 = { "TST1", "TST2", "TST3"};

    List<List<String>> l = new ArrayList<>();

    for (var i: Arrays.asList(0, 1, 2, 3)) {

        l.addAll(nCr(array2, i));

    }

    System.out.println(l);

}


private static List<List<String>> nCr(String[] array, int r) {

    List<List<String>> result = new ArrayList<>();

    if (r == 0) return result;

    if (r == 1) return nC1(array);


    for (int i = 0; i < array.length - r + 1; i++) {

        List<List<String>> result2 = nCr(

                Arrays.copyOfRange(array, i + 1, array.length),

                r - 1);

        for (var x: result2 ) {

            x.add(array[i]);

            result.add(x);

        }

    }

    return result;

}


private static List<List<String>> nC1(String[] array) {

    List<List<String>> l = new ArrayList<>();

    for (var x: array) {

        l.add(new ArrayList<>(Arrays.asList(x)));

    }

    return l;

}

輸出:


[[TST1], [TST2], [TST3], [TST2, TST1], [TST3, TST1], [TST3, TST2], [TST3, TST2, TST1]]



查看完整回答
反對(duì) 回復(fù) 2024-01-05
  • 1 回答
  • 0 關(guān)注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)