<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 在矩陣中查找特定對 > 原文: [https://www.geeksforgeeks.org/find-a-specific-pair-in-matrix/](https://www.geeksforgeeks.org/find-a-specific-pair-in-matrix/) 給定一個整數的 nxn 矩陣 mat [n] [n],求出所有索引選擇的 mat(c,d)– mat(a,b)的最大值,以使 c > a 和 d > b。 **示例**: ``` Input: mat[N][N] = {{ 1, 2, -1, -4, -20 }, { -8, -3, 4, 2, 1 }, { 3, 8, 6, 1, 3 }, { -4, -1, 1, 7, -6 }, { 0, -4, 10, -5, 1 }}; Output: 18 The maximum value is 18 as mat[4][2] - mat[1][0] = 18 has maximum difference. ``` 該程序應只對矩陣進行一次遍歷。 即預期時間復雜度為 `O(n^2)` 一種簡單的**解決方案**是應用暴力。 對于矩陣中的所有值 mat(a,b),我們找到具有最大值的 mat(c,d),使得 c > a 和 d > b 并繼續更新到目前為止找到的最大值。 我們最終返回最大值。 以下是其實現。 ## C++ ```cpp // A Naive method to find maximum value of mat[d][e] // - ma[a][b] such that d > a and e > b #include <bits/stdc++.h> using namespace std; #define N 5 // The function returns maximum value A(d,e) - A(a,b) // over all choices of indexes such that both d > a // and e > b. int findMaxValue(int mat[][N]) { ????// stores maximum value ????int maxValue = INT_MIN; ????// Consider all possible pairs mat[a][b] and ????// mat[d][e] ????for (int a = 0; a < N - 1; a++) ????for (int b = 0; b < N - 1; b++) ????????for (int d = a + 1; d < N; d++) ????????for (int e = b + 1; e < N; e++) ????????????if (maxValue < (mat[d][e] - mat[a][b])) ????????????????maxValue = mat[d][e] - mat[a][b]; ????return maxValue; } // Driver program to test above function int main() { int mat[N][N] = { ????????????????{ 1, 2, -1, -4, -20 }, ????????????????{ -8, -3, 4, 2, 1 }, ????????????????{ 3, 8, 6, 1, 3 }, ????????????????{ -4, -1, 1, 7, -6 }, ????????????????{ 0, -4, 10, -5, 1 } ????????????}; ????cout << "Maximum Value is " ????????<< findMaxValue(mat); ????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>

                              哎呀哎呀视频在线观看