C#的思路求解
这个怎么写啊,求思路?高手帮帮忙!
[ 本帖最后由 宇智波曌 于 2011-10-19 13:44 编辑 ]
程序代码:Console.WriteLine("Input:");
int n = int.Parse(Console.ReadLine());
int[,] result = new int[n,n];
int no = 1, direction = 0,x = 0,y=0;
for (int i = 0; i < n * n; i++)
{
result[x, y] = no;
if (direction == 0)
{
if (y +1 == n || result[x,y+1]!=0)
{
direction = 1;
x++;
}
else
y++;
}
else if (direction == 1)
{
if (x + 1 == n || result[x+1,y]!= 0)
{
direction = 2;
y--;
}
else
x++;
}
else if (direction == 2)
{
if (y == 0 || result[x, y - 1] != 0)
{
direction = 3;
x--;
}
else
y--;
}
else if (direction == 3)
{
if (result[x - 1, y] != 0)
{
direction = 0;
y++;
}
else
x--;
}
no++;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(result[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();