<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 給定一個 n x n 方陣,求出大小為 k x k 的所有子方和 > 原文: [https://www.geeksforgeeks.org/given-n-x-n-square-matrix-find-sum-sub-squares-size-k-x-k/](https://www.geeksforgeeks.org/given-n-x-n-square-matrix-find-sum-sub-squares-size-k-x-k/) 給定一個 n x n 的正方形矩陣,求出所有大小為 k x k 的子正方形的和,其中 k 小于或等于 n。 **示例**: ``` Input: n = 5, k = 3 arr[][] = { {1, 1, 1, 1, 1}, {2, 2, 2, 2, 2}, {3, 3, 3, 3, 3}, {4, 4, 4, 4, 4}, {5, 5, 5, 5, 5}, }; Output: 18 18 18 27 27 27 36 36 36 Input: n = 3, k = 2 arr[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, }; Output: 12 16 24 28 ``` **簡單解決方案**是所有可能子正方形的一個接一個拾取起點(最左上角)。 選取起點后,從選取的起點開始計算子平方和。 以下是該想法的實現。 ## C++ ```cpp // A simple C++ program to find sum of all subsquares of size k x k #include <iostream> using namespace std; // Size of given matrix #define n 5 // A simple function to find sum of all sub-squares of size k x k // in a given square matrix of size n x n void printSumSimple(int mat[][n], int k) { ???// k must be smaller than or equal to n ???if (k > n) return; ???// row number of first cell in current sub-square of size k x k ???for (int i=0; i<n-k+1; i++) ???{ ??????// column of first cell in current sub-square of size k x k ??????for (int j=0; j<n-k+1; j++) ??????{ ??????????// Calculate and print sum of current sub-square ??????????int sum = 0; ??????????for (int p=i; p<k+i; p++) ?????????????for (int q=j; q<k+j; q++) ?????????????????sum += mat[p][q]; ???????????cout << sum << "? "; ??????} ??????// Line separator for sub-squares starting with next row ??????cout << endl; ???} } // Driver program to test above function int main() { ????int mat[n][n] = {{1, 1, 1, 1, 1}, ?????????????????????{2, 2, 2, 2, 2}, ?????????????????????{3, 3, 3, 3, 3}, ?????????????????????{4, 4, 4, 4, 4}, ?????????????????????{5, 5, 5, 5, 5}, ????????????????????}; ????int k = 3; ????printSumSimple(mat, 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>

                              哎呀哎呀视频在线观看