我们已经给出了一个奇数阶的螺旋矩阵,在这个矩阵中,我们以数字1为中心,顺时针向右移动。 例如:
null
Input : n = 3 Output : 25Explanation : spiral matrix = 7 8 96 1 25 4 3The sum of diagonals is 7+1+3+9+5 = 25Input : n = 5Output : 101Explanation : spiral matrix of order 521 22 23 23 2520 7 8 9 1019 6 1 2 1118 5 4 3 1217 16 15 14 13The sum of diagonals is 21+7+1+3+13+25+9+5+17 = 101
如果我们仔细观察nxn的螺旋矩阵,我们可以注意到右上角元素的值为n 2. .左上角的值是(n^2)-(n-1)[为什么?不是因为我们在螺旋矩阵中顺时针移动蚂蚁,所以我们从右上角减去n-1后得到左上角的值]。类似地,左下角的值为(n^2)–2(n-1),右下角的值为(n^2)–3(n-1)。加上所有四个角,我们得到4[(n^2)]–6(n-1)。 设f(n)是nxn矩阵的对角元素之和。利用上述观察结果,我们可以递归地将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++
// 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; } |
JAVA
// Java program to find sum of // diagonals of spiral matrix class GFG { // function returns sum of diagonals static 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 to test public static void main (String[] args) { int n = 7 ; System.out.print(spiralDiaSum(n)); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to find sum of # diagonals of spiral matrix # function returns sum of diagonals def 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 )) # Driver program n = 7 ; print (spiralDiaSum(n)) # This code is contributed by Anant Agarwal. |
C#
// C# program to find sum of // diagonals of spiral matrix using System; class GFG { // function returns sum of diagonals static 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 code public static void Main (String[] args) { int n = 7; Console.Write(spiralDiaSum(n)); } } // This code is contributed by parashar... |
PHP
<?php // PHP program to find sum of // diagonals of spiral matrix // function returns sum // of diagonals function 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)); } // Driver Code $n = 7; echo spiralDiaSum( $n ); // This code is contributed by anuj_67. ?> |
Javascript
<script> // Javascript program to find sum of // diagonals of spiral matrix // function returns sum of diagonals function 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)); } // Driver program let n = 7; document.write(spiralDiaSum(n)); </script> |
输出:
261
本文由 希瓦姆·普拉丹(anuj_charm) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END