用C#语言编写一个5*5魔方阵
时间: 2024-03-17 11:45:39 浏览: 130
C#魔方阵源代码
4星 · 用户满意度95%
好的,以下是用C#语言编写一个5*5魔方阵的代码:
```csharp
using System;
class MagicSquare
{
static void Main()
{
int[,] magicSquare = new int[5,5];
int number = 1;
int row = 0;
int column = 2;
magicSquare[row, column] = number;
while (number < 25)
{
number++;
row--;
column++;
if (row < 0 && column > 4)
{
row += 2;
column--;
}
else if (row < 0)
{
row = 4;
}
else if (column > 4)
{
column = 0;
}
if (magicSquare[row, column] != 0)
{
row += 2;
column--;
}
magicSquare[row, column] = number;
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write("{0,2} ", magicSquare[i,j]);
}
Console.WriteLine();
}
}
}
```
运行该程序,即可输出一个5*5的魔方阵。
阅读全文