C#陣列相乘

iamzxf發表於2015-04-22


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace matrixMutiply
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arrayA = new int[,] { { 8, 3, 4 }, { 1, 5, 9 }, { 6, 7, 2 } };
            int[,] arrayB = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            int[,] arrayC = new int[3, 3];

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    for (int k = 0; k < 3; k++)
                    {
                        arrayC[i, j] += arrayA[i,k] * arrayB[k,j];
                    }
                }
            }

            Console.WriteLine("矩陣A:");
            displayMatrix(arrayA);
            Console.WriteLine("矩陣B");
            displayMatrix(arrayB);
            Console.WriteLine("矩陣C");
            displayMatrix(arrayC);

            Console.ReadLine();
            
        }

        public static void displayMatrix(int[,]matrix)
        {
            for(int i=0;i<matrix.GetLength(0);i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    Console.Write("{0,4}", matrix[i, j]);  
                }
                Console.WriteLine();
            }
        }
    }
}



相關文章