以螺旋顺序打印阵列 [英] Printing an Array in Spiral Order

查看:132
本文介绍了以螺旋顺序打印阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以螺旋顺序打印数组。通过以螺旋顺序打印数组,我修改了代码为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?

推荐答案

将二维矩阵转换为螺旋顺序 [ ^ ]


这篇关于以螺旋顺序打印阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆