62,266
社区成员
发帖
与我相关
我的任务
分享
public 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[j, k++] = index++;
k--;
j++;
while (j <= bottom)
array[j++, k] = index++;
j--;
k--;
while (k >= left)
array[j, k--] = index++;
k++;
j--;
while (j >= top)
array[j--, k] = index++;
j++;
k++;
right--;
left++;
bottom--;
top++;
}
Print(array);
}
static void Print(int[,] array)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
Console.Write(array[i, j].ToString().PadLeft(array.LongLength.ToString().Length, '0') + " ");
Console.WriteLine();
}
Console.WriteLine("-----");
}