螺旋数(C#)

返回一个螺旋数数组

 static  public int[,] SpiralNumSort(int num)
        {
            int temp = num % 2 > 0 ? (num / 2 + 1) : (num / 2);
            int[,] arynum = new int[num, num];
            int number = 0;
            for (int q = 0; q < temp; q++)
            {
                for (int i = q; i < num - q; i++)
                {
                    number++;
                    arynum[i, q] = number;
                }
                for (int i = q + 1; i < num - q; i++)
                {
                    number++;
                    arynum[num - q - 1, i] = number;
                }
                for (int i = num - 2 - q; i >= q; i--)
                {
                    number++;
                    arynum[i, num - 1 - q] = number;
                }
                for (int i = num - 2 - q; i > q; i--)
                {
                    number++;
                    arynum[q, i] = number;
                }
            }
            return arynum;
        }

类似于

1  8  7

2  9  6

3  4  5

这样的结果

author by:可乐

posted @ 2012-05-30 18:12  爱喝可乐  阅读(377)  评论(0编辑  收藏  举报