当n=2      n=3                 n=4                 n=...1  2       1  2  3             1  2  3  4
4  3       8  9  4             12 13 14 5
           7  6  5             11 16 15 6
                               10  9  8 7

解决方案 »

  1.   

    这在ACM题目里算简单的了
    有点取巧的办法就是建个大二维数组 然后逐个填充各个位就可以了
      

  2.   

    using System;
    namespace Temp
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class Class1
    {
    static int n = 5;
        static int[,] Num;  /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    Class1 Show = new Class1();
    Show.ShowNumber(n);
    System.Console.Read();

    } void ShowNumber(int n)
    {
    int count = 0;
    int x=1;
    int y=n/2;
    Num = new int[n,n];
    if ((n % 2)!=0)
    {
    Num[y,y] = n*n;
    }
    for(int i=0;i<4;i++)
    {
    for(int j=count;j<n-count-1;j++)
    {
    Num[count,j]=x++;
    }
    for(int j=count;j<n-count-1;j++)
    {
    Num[j,n-count-1]=x++;
    }
    for(int j=n-count-1;j>count;j--)
    {
    Num[n-count-1,j]=x++;
    }
    for(int j=n-count-1;j>count;j--)
    {
    Num[j,count]=x++;
    }
    count++;
    }
    for(int i=0;i<n;i++)
    {
    for(int j=0;j<n;j++)
    {
    System.Console.Write(Num[i,j]+"    ");
    }
    System.Console.Write("\n");
    }
    }
    }
    }
      

  3.   

    试一试:private void DoTest(int vNum) //vNum = n

    int[,] ldata;
    int ltmp = 0,i,n;
    ldata = new int[vNum,vNum];
    for(n=1;n <= vNum*2;n++)
    {
    i=n/2 - n/4;
    for(;i<vNum - n/4;i++)
    {
    ltmp ++;
    ldata[(n-1)/4,i] = ltmp;
    }
    RotateArray(ref ldata);
    }
    for(n=1;n<=vNum%2 *2;n++)
    RotateArray(ref ldata);
    for(n=0;n<vNum;n++) 
    {
    for(i=0;i<vNum;i++)
    System.Console.Write(ldata[n,i].ToString() + "\t");  
    System.Console.WriteLine();
    }
    } private void RotateArray(ref int[,] vData)
    {
    int[,] ldata;
    ldata = new int[vData.GetLength(0),vData.GetLength(1)];
    for(int n=0;n<vData.GetLength(0);n++)
    for(int i=0;i<vData.GetLength(1);i++)
    {
    ldata[vData.GetLength(1) - 1 - i,n] = vData[n,i];
    }
    vData = ldata;
    }
      

  4.   

    lgyangell() : 试一下showNumber(n>=10),有问题;
      

  5.   

    不会吧~~我回家试试~公司没有.net!~~~晚上时间短没有自己检查~~
      

  6.   

    private class FillArrays
    {
    public FillArrays(int n)
    {
    this.n = n;
    num = new int[n,n];
    this.Fill();
    }

    private int n;
    private int x = 0;
    private int y = 0;
    private int[,] num;
    PointTo pt = PointTo.Right ;
                
    public int[,] Result 
    {
    get
    {
    return num;
    }
    } private void PointToRight()
    {
    pt = PointTo.Right;
    y++;
    x++;
    } private void PointToLeft()
    {
    pt = PointTo.Left;
    y--;
    x--;
    } private void PointToDown()
    {
    pt = PointTo.Down;
    x++;
    y--;
    } private void PointToUp()
    {
    pt = PointTo.Up;
    x--;
    y++;
    } private void Fill()
    {
    for(int i=1; i<= n*n; i++)
    {
    do
    {
    if (pt == PointTo.Right)
    {
    if (y == n)
    PointToDown();
    else
    {
    if (num[x,y] == 0)
    {
    num[x,y] = i;
    y++;
    break;
    }
    else
    PointToDown();
    }

    }
    if (pt == PointTo.Down)
    {
    if (x == n)
    PointToLeft();
    else
    {
    if (num[x,y] == 0)
    {
    num[x,y] = i;
    x++;
    break;
    }
    else
    PointToLeft();
    }
    }
    if (pt == PointTo.Left)
    {
    if (y < 0)
    PointToUp();
    else
    {
    if (num[x,y] == 0)
    {
    num[x,y] = i;
    y--;
    break;
    }
    else
    PointToUp();
    }
    }
    if (pt == PointTo.Up)
    {
    if (x < 0)
    PointToRight();
    else
    {
    if (num[x,y] == 0)
    {
    num[x,y] = i;
    x--;
    break;
    }
    else
    PointToRight();
    }
    }
    }
    while(true);
    }


    }
      

  7.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication1
    {
    class Program
    {
    static int width;
    static int square;
    static void Main(string[] args)
    {
    int n;
    while ((n = Convert.ToInt32(Console.ReadLine())) != 0)
    {
    square = n;
    int idx = 1;
    width = (n * n).ToString().Length + 1;
    Console.Clear();
    for (int i = 0; i < (n + 1) / 2; i++)
    {
    Print(n - i * 2, ref idx);
    }
    }
    } private static void Print(int n, ref int idx)
    {
    int lt = (square - n) / 2  ;
    if (n == 1)
    {
    Console.SetCursorPosition(lt * width, lt);
    Console.Write(idx++);
    Console.SetCursorPosition(0, square);
    }
    for (int i = 0; i < n-1; i++)
    {
    Console.SetCursorPosition((lt+i)*width, lt);
    Console.Write(idx++);
    }
    for (int i = 0; i < n - 1; i++)
    {
    Console.SetCursorPosition((lt + n - 1)*width, lt + i);
    Console.WriteLine(idx++);
    }
    for (int i = 0; i < n - 1; i++)
    {
    Console.SetCursorPosition((lt + n - 1 - i )* width, lt + n - 1);
    Console.WriteLine(idx++);
    }
    for (int i = 0; i < n - 1; i++)
    {
    Console.SetCursorPosition(lt*width, lt + n - 1 - i);
    Console.WriteLine(idx++);
    if (n == 2)
    Console.SetCursorPosition(0, square);
    }
    }
    }
    }
      

  8.   

    pupo(泡泡) ,请教一下,PointTo是哪一个命名空间的?我找了好久都没找到,真晕哦!