【问题标题】:Printing out a matrix and its transpose, C#打印出一个矩阵及其转置,C#
【发布时间】:2013-08-25 17:27:40
【问题描述】:

我有一个程序可以创建一个转置 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

但我只能打印出转置矩阵。我该如何解决这个问题?

【问题讨论】:

    标签: c# algorithm matrix transpose


    【解决方案1】:

    在计算转置后,您可以同时打印两条线。

     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();
        }
    

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 2017-05-30
      • 2020-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多