How do you rotate a two dimensional array?

后端 未结 30 3119
耶瑟儿~
耶瑟儿~ 2020-11-22 02:43

Inspired by Raymond Chen\'s post, say you have a 4x4 two dimensional array, write a function that rotates it 90 degrees. Raymond links to a solution in pseudo code, but I\'d

30条回答
  •  长发绾君心
    2020-11-22 03:33

    C# code to rotate [n,m] 2D arrays 90 deg right

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MatrixProject
    {
        // mattrix class
    
        class Matrix{
            private int rows;
            private int cols;
            private int[,] matrix;
    
            public Matrix(int n){
                this.rows = n;
                this.cols = n;
                this.matrix = new int[this.rows,this.cols];
    
            }
    
            public Matrix(int n,int m){
                this.rows = n;
                this.cols = m;
    
                this.matrix = new int[this.rows,this.cols];
            }
    
            public void Show()
            {
                for (var i = 0; i < this.rows; i++)
                {
                    for (var j = 0; j < this.cols; j++) {
                        Console.Write("{0,3}", this.matrix[i, j]);
                    }
                    Console.WriteLine();
                }                
            }
    
            public void ReadElements()
            {
               for (var i = 0; i < this.rows; i++)
                    for (var j = 0; j < this.cols; j++)
                    {
                        Console.Write("element[{0},{1}]=",i,j);
                        this.matrix[i, j] = Convert.ToInt32(Console.ReadLine());
                    }            
            }
    
    
            // rotate [n,m] 2D array by 90 deg right
            public void Rotate90DegRight()
            {
    
                // create a mirror of current matrix
                int[,] mirror = this.matrix;
    
                // create a new matrix
                this.matrix = new int[this.cols, this.rows];
    
                for (int i = 0; i < this.rows; i++)
                {
                    for (int j = 0; j < this.cols; j++)
                    {
                        this.matrix[j, this.rows - i - 1] = mirror[i, j];
                    }
                }
    
                // replace cols count with rows count
                int tmp = this.rows;
                this.rows = this.cols;
                this.cols = tmp;           
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Matrix myMatrix = new Matrix(3,4);
                Console.WriteLine("Enter matrix elements:");
                myMatrix.ReadElements();
                Console.WriteLine("Matrix elements are:");
                myMatrix.Show();
                myMatrix.Rotate90DegRight();
                Console.WriteLine("Matrix rotated at 90 deg are:");
                myMatrix.Show();
                Console.ReadLine();
            }
        }
    }
    

    Result:

        Enter matrix elements:
        element[0,0]=1
        element[0,1]=2
        element[0,2]=3
        element[0,3]=4
        element[1,0]=5
        element[1,1]=6
        element[1,2]=7
        element[1,3]=8
        element[2,0]=9
        element[2,1]=10
        element[2,2]=11
        element[2,3]=12
        Matrix elements are:
          1  2  3  4
          5  6  7  8
          9 10 11 12
        Matrix rotated at 90 deg are:
          9  5  1
         10  6  2
         11  7  3
         12  8  4
    

提交回复
热议问题