矩阵螺旋输出
曾经的一道面试题目,没事重新写了一下,算法很简单。但是总觉得还有比这更优秀的算法,欢迎大家来拍砖!!目的想找到更加好的算法!!
矩阵螺旋输出
class Program
{
static void Main(string[] args)
{
//数组arr是一个n*n的二维数组
int n = 5, m = 10;
int[,] arr = new int[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
arr[i, j] = m++;
}
}
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(" ");
Console.Write(arr[i, j]);
}
Console.WriteLine();
Console.WriteLine();
}
OutputMatrix(arr, n);
Console.ReadLine();
}
static void OutputMatrix(int[,] arr, int n)
{
int row = 0, col = 0, circle = 0, sum = Convert.ToInt32(Math.Floor(Convert.ToDouble(n / 2)));
StringBuilder str = new StringBuilder();
while (circle < sum + 1)
{
for (; row < n - circle; row++)
str.Append(arr[row, col].ToString() + ",");
row--;
col++;
for (; col < n - circle; col++)
str.Append(arr[row, col].ToString() + ",");
row--;
col--;
for (; row >= circle; row--)
str.Append(arr[row, col].ToString() + ",");
row++;
col--;
for (; col > circle; col--)
str.Append(arr[row, col].ToString() + ",");
row++;
col++;
circle++;
}
Console.WriteLine(" "+str.ToString());
}
}
class Program
{
static void Main(string[] args)
{
//数组arr是一个n*n的二维数组
int n = 5, m = 10;
int[,] arr = new int[n, n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
arr[i, j] = m++;
}
}
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(" ");
Console.Write(arr[i, j]);
}
Console.WriteLine();
Console.WriteLine();
}
OutputMatrix(arr, n);
Console.ReadLine();
}
static void OutputMatrix(int[,] arr, int n)
{
int row = 0, col = 0, circle = 0, sum = Convert.ToInt32(Math.Floor(Convert.ToDouble(n / 2)));
StringBuilder str = new StringBuilder();
while (circle < sum + 1)
{
for (; row < n - circle; row++)
str.Append(arr[row, col].ToString() + ",");
row--;
col++;
for (; col < n - circle; col++)
str.Append(arr[row, col].ToString() + ",");
row--;
col--;
for (; row >= circle; row--)
str.Append(arr[row, col].ToString() + ",");
row++;
col--;
for (; col > circle; col--)
str.Append(arr[row, col].ToString() + ",");
row++;
col++;
circle++;
}
Console.WriteLine(" "+str.ToString());
}
}
当然还有另一种形式的输入方法:
矩阵螺旋输出
class Program
{
static void Main(string[] args)
{
//数组arr是一个n*n的二维数组
int n = 5, m = 10;
int [,] arr=new int [n,n];
int row = 0, col = 0, circle = 0, sum = Convert.ToInt32(Math.Floor(Convert.ToDouble(n / 2)));
while (circle < sum + 1)
{
for (; row < n - circle; row++)
arr[row, col] = m++;
row--;
col++;
for (; col < n - circle; col++)
arr[row, col] = m++;
row--;
col--;
for (; row >= circle; row--)
arr[row, col] = m++;
row++;
col--;
for (; col > circle; col--)
arr[row, col] = m++;
row++;
col++;
circle++;
}
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(" ");
Console.Write(arr[i, j]);
}
Console.WriteLine();
Console.WriteLine();
}
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
//数组arr是一个n*n的二维数组
int n = 5, m = 10;
int [,] arr=new int [n,n];
int row = 0, col = 0, circle = 0, sum = Convert.ToInt32(Math.Floor(Convert.ToDouble(n / 2)));
while (circle < sum + 1)
{
for (; row < n - circle; row++)
arr[row, col] = m++;
row--;
col++;
for (; col < n - circle; col++)
arr[row, col] = m++;
row--;
col--;
for (; row >= circle; row--)
arr[row, col] = m++;
row++;
col--;
for (; col > circle; col--)
arr[row, col] = m++;
row++;
col++;
circle++;
}
Console.WriteLine();
Console.WriteLine();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(" ");
Console.Write(arr[i, j]);
}
Console.WriteLine();
Console.WriteLine();
}
Console.ReadLine();
}
}
效果图: