Write a routine that prints out a 2-D array in spiral order
2012-04-05 21:58
169 查看
我的理解,给定2维数组
1 2 3三角形判断
8 9 4
7 6 5
“螺旋式”顺序输出:1 2 3 4 5 6 7 8 9
1 2 3三角形判断
8 9 4
7 6 5
“螺旋式”顺序输出:1 2 3 4 5 6 7 8 9
private static void PrintNumberasSpiralOrder() { // 1 2 3 4 // 12 13 14 5 // 11 16 15 6 // 10 9 8 7 // 1, 2 , 3, 4, 5 // 16, 17, 18, 19, 6 // 15, 24, 25, 20, 7 // 14, 23, 22, 21, 8 // 13, 12, 11, 10, 9 int [,] a = new int [,] { { 1 , 2 , 3 , 4 , 5 } , { 16 , 17 , 18 , 19 , 6 } , { 15 , 24 , 25 , 20 , 7 } , { 14 , 23 , 22 , 21 , 8 } , { 13 , 12 , 11 , 10 , 9 } } ; // int[,] a = new int[,] { { 1, 2, 3, 4 }, { 12, 13, 14, 5 }, { 11, 16, 15, 6 }, { 10, 9, 8, 7 } }; // int[,] a = new int[,] { { 1, 2 }, {4,3} }; const int N = 5 ; // spiral for ( int i = N - 1 , j = 0 ; i > 0 ; i -- , j ++ ) { // i 从最大维度数开始 // j 从最小维度数开始 // i--来缩小范围 // j++来缩小范围 // k 增长缩小因子 for ( int k = j; k < i; k ++ ) System.Console.Write( " {0} " , a[j, k]); Console.WriteLine( "" ); for ( int k = j; k < i; k ++ ) System.Console.Write( " {0} " , a[k, i]); Console.WriteLine( "" ); for ( int k = i; k > j; k -- ) System.Console.Write( " {0} " , a[i, k]); Console.WriteLine( "" ); for ( int k = i; k > j; k -- ) System.Console.Write( " {0} " , a[k, j]); Console.WriteLine( "" ); } // special case for middle element if N is odd // 如果是奇数,则要条印中间的数。 if (N % 2 == 1 ) System.Console.Write(a[(N - 1 ) / 2 , (N - 1 ) / 2 ]); }
相关文章推荐
- Write a program that gives count of common characters presented in an array of strings..(or array of
- Ruby: Sorting an array of strings, in alphabetical order, that includes some arrays of strings
- Write a program that gives count of common characters presented in an array of strings..(or array of
- Write a program that prints its input one word per line.
- We have detected that MySQL products under the Commercial license are installed. In order to proceed with this GPL installation these Commercial
- Search a 2D Matrix IIWrite an efficient algorithm that searches for a value in an m x n matrix. This
- Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
- Array-Construct Binary Tree from Preorder and Inorder Traversal
- Pseudo code for In-class QuestionExample 3: Write pseudo code that performs the following: Ask a use
- LeetCode刷题【Array】 Construct Binary Tree from Preorder and Inorder Traversal
- 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.
- Array-Construct Binary Tree from Inorder and Postorder Traversal
- 逆序输出字符串 print the array in reverse order
- how to write order by and limit query in jpa [duplicate]
- Data Structure Binary Tree: Level order traversal in spiral form
- recursion - How to write a simple preorder DOM tree traversal algorithm in jQuery? - Stack Overflow
- Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
- How to know that a triangle triple exists in one array?
- CareerCup Write the code to find lexicographic minimum in a circular array
- 【leetcode】Array——Construct Binary Tree from Preorder and Inorder Traversal(105)