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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 矩陣的中心元素等于對角線的一半 > 原文: [https://www.geeksforgeeks.org/center-element-of-matrix-equals-sums-of-half-diagonals/](https://www.geeksforgeeks.org/center-element-of-matrix-equals-sums-of-half-diagonals/) 給定一個奇數階矩陣,即(5 * 5)。 任務是檢查矩陣的中心元素是否等于所有半對角線的總和。 ![](https://img.kancloud.cn/51/58/5158def8bbae2fbfd0ef5f0d6423f1de_300x162.png) **示例**: ``` Input : mat[][] = { 2 9 1 4 -2 6 7 2 11 4 4 2 9 2 4 1 9 2 4 4 0 2 4 2 5 } Output :Yes Explanation : Sum of Half Diagonal 1 = 2 + 7 = 9 Sum of Half Diagonal 2 = 9 + 0 = 9 Sum of Half Diagonal 3 = 11 + -2 = 9 Sum of Half Diagonal 4 = 5 + 4 = 9 Here, All the sums equal to the center element that is mat[2][2] = 9 ``` **簡單方法**: 迭代兩個循環,找到所有對角線總和,然后檢查所有總和是否等于矩陣的中心元素。 如果其中任何一個不等于中心元素,則打印“否”,否則打印“是”。 時間復雜度:O(N * N) **有效方法**:基于[有效方法來找到`O(n)`](https://www.geeksforgeeks.org/efficiently-compute-sums-of-diagonals-of-a-matrix/)中的對角線總和。 以下是此方法的實現 ## C++ ```cpp // C++ Program to check if the center // element is equal to the individual? // sum of all the half diagonals #include <stdio.h> #include<bits/stdc++.h> using namespace std; const int MAX = 100; // Function to Check center element? // is equal to the individual? // sum of all the half diagonals bool HalfDiagonalSums(int mat[][MAX], int n) {???? ????// Find sums of half diagonals ????int diag1_left = 0, diag1_right = 0; ????int diag2_left = 0, diag2_right = 0;???? ????for (int i = 0, j = n - 1; i < n; i++, j--) { ????????if (i < n/2) { ????????????diag1_left += mat[i][i]; ????????????diag2_left += mat[j][i];??????????? ????????} ????????else if (i > n/2) { ????????????diag1_right += mat[i][i]; ????????????diag2_right += mat[j][i];??????????? ????????} ????} ????return (diag1_left == diag2_right &&? ????????????diag2_right == diag2_left && ????????????diag1_right == diag2_left && ????????????diag2_right == mat[n/2][n/2]); } // Driver code int main() { ????int a[][MAX] = { { 2, 9, 1, 4, -2}, ?????????????????????{ 6, 7, 2, 11, 4},? ?????????????????????{ 4, 2, 9, 2, 4}, ?????????????????????{ 1, 9, 2, 4, 4}, ?????????????????????{ 0, 2, 4, 2, 5} }; ????????cout << ( HalfDiagonalSums(a, 5) ? "Yes" : "No" ); ????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>

                              哎呀哎呀视频在线观看