以螺旋顺序打印阵列

以螺旋顺序打印阵列

问题描述:

我想以螺旋顺序打印数组。通过以螺旋顺序打印数组,我修改了代码为C#。

I want to print an array in spiral order. By the Printing an Array in Spiral Order, I modified the code as C#.

static void Main(string[] args)
       {
           int[,] a = new int[,]
           {
               {1,2,3,4},
               {5,6,7,8},
               {9,10,11,12},
               {13,14,15,16}
           };
           int size = 16;
           PrintInSpiral(a, size);
           Console.ReadLine();
       }
       public static void PrintInSpiral(int[,] numbers, int size)
       {
           for (int i = size - 1, j = 0; i >= 0; i--, j++)
           {
               for (int k = j; k < i; k++)
                   Console.WriteLine(numbers[j, k] + " ");
               for (int k = j; k < i; k++)
                   Console.WriteLine(numbers[k, i] + " ");
               for (int k = i; k > j; k--)
                   Console.WriteLine(numbers[i, k] + " ");
               for (int k = i; k > j; k--)
                   Console.WriteLine(numbers[k, j] + " ");
           }
       }



但是它不正确(超出索引的例外),请你帮我查一下算法和代码?


However it is incorrect(exception for out of index), would you please help me to check the algorithm and code?