C#数组相乘

来源:互联网 发布:淘宝的全球购是真的吗 编辑:程序博客网 时间:2024/05/19 14:34


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



0 0
原创粉丝点击