九宫格扩展,输入一个奇数,得到横竖斜相加相等结果

  static   public int[,] NineNumSort(int num)
        {
            int[,] arynum = new int[num, num];
            int max = num - 1;
            int x = 0;
            int y = 0;
            arynum[x, num / 2] = 1;
            y = num / 2;
            for (int i = 2; i <= num * num; i++)
            {
                if (x == 0)
                {
                    if (y + 1 <= max)
                    {
                        arynum[max, y + 1] = i;
                        y++;
                        x = max;
                        continue;
                    }
                    else
                    {
                        arynum[x + 1, max] = i;
                        x++;
                        y = max;
                        continue;
                    }
                }

                if (y == max)
                {
                    arynum[x - 1, 0] = i;
                    x--;
                    y = 0;
                    continue;
                }

                if (arynum[x - 1, y + 1] != 0)
                {
                    arynum[x + 1, y] = i;
                    x++;
                    continue;
                }
                else
                {
                    arynum[x - 1, y + 1] = i;
                    x--;
                    y++;
                    continue;
                }
            }
            return arynum;
        }

输入一个奇数,得到一个正方形  横向竖向斜向 相加相等的 function

author by:可乐

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