本帖最后由 pengpengabcd 于 2010-04-06 21:49:55 编辑

解决方案 »

  1.   

    http://topic.csdn.net/u/20080826/13/9e609334-2688-49cc-b0bf-b93534d3f78b.html
      

  2.   


            static void Main(string[] args)
            {
                PrintBox(5);
            }        static void PrintBox(int row)
            {
                int[,] array = new int[row, row];
                int index = 1;
                int j = 0, k = 0;            #region first
                int right = row - 1, left = 0, bottom = row - 1, top = 1;
                for (int i = 0; i < row / 2 + row % 2; i++)
                {
                    while (k <= right)
                        array[k++, j] = index++;
                    k--;
                    j++;
                    while (j <= bottom)
                        array[k, j++] = index++;
                    j--;
                    k--;
                    while (k >= left)
                        array[k--, j] = index++;
                    k++;
                    j--;
                    while (j >= top)
                        array[k, j--] = index++;
                    j++;
                    k++;
                    right--;
                    left++;
                    bottom--;
                    top++;
                }
                for (int a = 0; a < row; a++)
                {
                    for (int b = 0; b < row; b++)
                        Console.Write(array[a, b].ToString().PadLeft(2, '0') + " ");
                    Console.WriteLine();
                }
                Console.WriteLine("-----");
                #endregion        }
      

  3.   

    添加引用System.Drawing;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;namespace ConsoleApplication1
    {
        public enum Direction
        {
            Down,
            Right,
            Up,
            Left
        }
        class ScrewSqure
        {
            //根据当前的方向计算下一个方向
            private static Direction ChangeDirection(Direction dir)
            {
                int d = (int)dir;
                return (Direction)((++d) % 4);
            }
            private static bool PosIsValid(int[,] data, Point p)
            {
                int n = data.GetLength(0);
                if (p.X < 0 || p.X >= n) return false;
                if (p.Y < 0 || p.Y >= n) return false;
                if (data[p.X, p.Y] != 0) return false;
                return true;
            }
            private static void GetNextPos(int[,] data, ref Point p, ref Direction dir)
            {
                int n = data.GetLength(0);
                Point next = new Point();
                for (; ; )
                {
                    next.X = p.X;
                    next.Y = p.Y;
                    switch (dir)
                    {
                        case Direction.Down:
                            next.X++;
                            break;
                        case Direction.Right:
                            next.Y++;
                            break;
                        case Direction.Up:
                            next.X--;
                            break;
                        case Direction.Left:
                            next.Y--;
                            break;
                        default:
                            break;
                    }
                    if (!PosIsValid(data, next))
                    {
                        dir = ChangeDirection(dir);
                    }
                    else break;
                }
                p.X = next.X;
                p.Y = next.Y;
            }
            private static void FillMatrics(int[,] data, Direction dir)
            {
                Point p = new Point();
                p.X = p.Y = 0;    
                int n = data.GetLength(0);
                for (int i = 1; i <= n*n; i++)
                {
                    data[p.X, p.Y] = i;
                    if (i < n * n)
                    {
                        GetNextPos(data, ref p, ref dir);
                    }
                }
            }
            static void Main(string[] args)
            {
                int[,] ar=new int[10,10];
                FillMatrics (ar,Direction.Down );
                for (int i = 0; i <ar.GetLength(0); i++)
                {
                    for (int j = 0; j < ar.GetLength (1); j++)
                    {
                        Console.Write("{0,4}", ar[i, j]);
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();
            }
        }
    }
    搞点。。
      

  4.   

    using System;class Program
    {
      static int[,] Fill(int n)
      {
        int d = 0;//↓, →, ↑, ←
        int[] di = { 1,  0, -1,  0 };
        int[] dj = { 0,  1,  0, -1 };
        int[,] a = new int[n, n];
        for (int i = 0, j = 0, k = 1; k <= n * n; i += di[d], j += dj[d], k++)
        {
          a[i, j] = k;
          if (i + di[d] < 0 || i + di[d] >= n || j + dj[d] < 0 || j + dj[d] >= n || a[i + di[d], j + dj[d]] > 0) d = (d + 1) % 4;
        }
        return a;
      }  static void Main()
      {
        int[,] a = Fill(5);
        for (int i = 0; i < a.GetLength(0); i++)
        {
          for (int j = 0; j < a.GetLength(1); j++)
            Console.Write("{0,3}", a[i, j]);
          Console.WriteLine();
        }
      }
    }
    /* 程序输出:
      1 16 15 14 13
      2 17 24 23 12
      3 18 25 22 11
      4 19 20 21 10
      5  6  7  8  9
    */