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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 螺旋奇數階方陣的兩個對角線之和 > 原文: [https://www.geeksforgeeks.org/sum-diagonals-spiral-odd-order-square-matrix/](https://www.geeksforgeeks.org/sum-diagonals-spiral-odd-order-square-matrix/) 我們給出了一個奇數階的螺旋矩陣,其中我們從 1 開始為中心,然后沿順時針方向向右移動。 **示例**: ``` Input : n = 3 Output : 25 Explanation : spiral matrix = 7 8 9 6 1 2 5 4 3 The sum of diagonals is 7+1+3+9+5 = 25 Input : n = 5 Output : 101 Explanation : spiral matrix of order 5 21 22 23 23 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13 The sum of diagonals is 21+7+1+3+13+ 25+9+5+17 = 101 ``` 如果我們仔細看一下 n x n 的螺旋矩陣,我們會注意到右上角元素的值為 n <sup>2</sup> 。 左上角的值為(n ^ 2)–(n-1)[為什么? 并不是說我們在螺旋矩陣中逆時針移動,因此在從右上角減去 n-1 之后,我們得到了左上角的值]。 同樣,左下角的值為(n ^ 2)– 2(n-1),右下角的值為(n ^ 2)– 3(n-1)。 在將所有四個角相加之后,我們得到 4 [(n ^ 2)] – 6(n-1)。 令 f(n)為 n x n 矩陣的對角元素之和。 使用上面的觀察,我們可以將 f(n)遞歸地寫為: ``` f(n) = 4[(n^2)] – 6(n-1) + f(n-2) ``` 從上述關系式中,我們可以借助迭代法求出螺旋矩陣的所有對角元素之和。 ``` spiralDiaSum(n) { if (n == 1) return 1; // as order should be only odd // we should pass only odd-integers return (4*n*n - 6*n + 6 + spiralDiaSum(n-2)); } ``` 下面是實現。 ## C++ ```cpp // C++ program to find sum of // diagonals of spiral matrix #include<bits/stdc++.h> using namespace std; // function returns sum of diagonals int spiralDiaSum(int n) { ????if (n == 1) ????????return 1; ????// as order should be only odd ????// we should pass only odd-integers ????return (4*n*n - 6*n + 6 + spiralDiaSum(n-2)); } // Driver program int main() { ????int n = 7; ????cout <<? spiralDiaSum(n); ????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>

                              哎呀哎呀视频在线观看