<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 來自兩個數組的 K 個最大和組合 > 原文: [https://www.geeksforgeeks.org/k-maximum-sum-combinations-two-arrays/](https://www.geeksforgeeks.org/k-maximum-sum-combinations-two-arrays/) 給定兩個大小相等的數組(`A`,`B`)和`N`(兩個數組的大小)。 通過將數組`A`中的一個元素與數組`B`中的另一個元素相加來構成**和組合**。從所有可能的和組合中顯示**最大`K`個有效和組合**。 例子: ``` Input : A[] : {3, 2} B[] : {1, 4} K : 2 [Number of maximum sum combinations to be printed] Output : 7 // (A : 3) + (B : 4) 6 // (A : 2) + (B : 4) Input : A[] : {4, 2, 5, 1} B[] : {8, 0, 3, 5} K : 3 Output : 13 // (A : 5) + (B : 8) 12 // (A : 4) + (B : 8) 10 // (A : 2) + (B : 8) ``` **方法 1(樸素算法)**: 我們可以通過將數組`A`中的一個元素和數組`B`中的另一個元素插入到最大堆中來進行所有可能的組合使用暴力。 在最大堆中,最大元素位于根節點,因此,每當我們從最大堆彈出時,我們都會獲得堆中存在的最大元素。 插入所有總和組合后,我們從最大堆中取出`K`個元素并顯示出來。 下面是上述方法的實現。 ## C++ ```cpp // A simple C++ program to find N maximum // combinations from two arrays, #include <bits/stdc++.h> using namespace std; // function to display first N maximum sum // combinations void KMaxCombinations(int A[], int B[], int N, ????????????????????????????????????????int K) { ????// max heap. ????priority_queue<int> pq; ????// insert all the possible combinations? ????// in max heap. ????for (int i = 0; i < N; i++) ????????for (int j = 0; j < N; j++) ????????????pq.push(A[i] + B[j]); ????// pop first N elements from max heap ????// and display them. ????int count = 0; ????while (count < K) { ????????cout << pq.top() << endl; ????????pq.pop(); ????????count++; ????} } // Driver Code. int main() { ????int A[] = { 4, 2, 5, 1 }; ????int B[] = { 8, 0, 5, 3 }; ????int N = sizeof(A)/sizeof(A[0]); ????int K = 3; ????KMaxCombinations(A, B, N, K); ????return 0; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看