我有一个创建转置 5x8 矩阵的程序。我创建了一个多维 5x8 数组,还创建了一个新数组来保存多维数组的转置。问题是,我首先想将原始矩阵写出到控制台,并且在同一行我希望写出转置。这是我的代码:
class Program
{
static void Main(string[] args)
{
int[,] matrix = new int[5, 8] {
{ 1, 2, 3, 4, 5,6,7,8 },
{ 9,10,11,12,13,14,15,16},
{ 17,18,19,20,21,22,23,24 },
{ 25,26,27,28,29,30,31,32 },
{ 33,34,35,36,37,38,39,40 },
};
for (int j = 0; j < 8; j++)
{
for (int r = 0; r < 5; r++)
Console.Write("{0} ", matrix[r, j]);
Console.WriteLine();
}
int[,] newArray = new int[8, 5];
for (int j = 0; j < 8; j++)
for (int r = 0; r < 5; r++)
newArray[j, r] = matrix[r, j];
Console.ReadLine();
}
}
我想要在控制台窗口中显示的是这样的: http://pbrd.co/19SXR0J
但我只能打印出转置矩阵。我该如何解决这个问题?
最佳答案
计算转置后,您可以同时打印两行。
for (int j = 0; j < 8; j++)
{
//write a line from the first matrix
for (int r = 0; r < 5; r++)
Console.Write("{0} ", matrix[r, j]);
//add some spaces for visual separation
Console.Write("\t\t");
//write a line from the transpose matrix
for (int r = 0; r < 5; r++)
Console.Write("{0} ", newArray[r, j]);
Console.WriteLine();
}
关于c# - 打印出矩阵及其转置,C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18431695/