using System;
namespace Test
{
public class T
{
public static void MultiplyMatrix()
{
Random ran = new Random();
int[,] a = new int[4, 5];
// Инициализируем данный массив
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
a[i, j] = ran.Next(1, 15);
}
}
int[,] b = new int[5, 4];
// Инициализируем данный массив
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
b[i, j] = ran.Next(1, 15);
}
}
int[,] c = new int[a.GetLength(0), b.GetLength(1)];
if (a.GetLength(1) == b.GetLength(0))
{
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
for (int k = 0; k < b.GetLength(0); k++)
{
c[i, j] += a[i, k] * b[k, j];
}
}
}
}
else
{
Console.WriteLine(
"\n Number of columns in First Matrix should be equal to Number of rows in Second Matrix.");
Console.WriteLine("\n Please re-enter correct dimensions.");
Environment.Exit(-1);
}
for (int i = 0; i < c.GetLength(0); i++)
{
for (int j = 0; j < c.GetLength(1); j++)
{
Console.Write("C[{0},{1}] : {2}", i, j, c[i, j]);
}
Console.WriteLine();
}
}
public static void Main()
{
MultiplyMatrix();
}
}
}
dXNpbmcgU3lzdGVtOwoKbmFtZXNwYWNlIFRlc3QKewogICAgcHVibGljIGNsYXNzIFQKICAgIHsKICAgICAgICBwdWJsaWMgc3RhdGljIHZvaWQgTXVsdGlwbHlNYXRyaXgoKQogICAgICAgIHsKICAgICAgICAgICAgUmFuZG9tIHJhbiA9IG5ldyBSYW5kb20oKTsKICAgICAgICAgICAgaW50WyxdIGEgPSBuZXcgaW50WzQsIDVdOwogICAgICAgICAgICAvLyDQmNC90LjRhtC40LDQu9C40LfQuNGA0YPQtdC8INC00LDQvdC90YvQuSDQvNCw0YHRgdC40LIKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBhLkdldExlbmd0aCgwKTsgaSsrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IGEuR2V0TGVuZ3RoKDEpOyBqKyspCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgYVtpLCBqXSA9IHJhbi5OZXh0KDEsIDE1KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpbnRbLF0gYiA9IG5ldyBpbnRbNSwgNF07CiAgICAgICAgICAgIC8vINCY0L3QuNGG0LjQsNC70LjQt9C40YDRg9C10Lwg0LTQsNC90L3Ri9C5INC80LDRgdGB0LjQsgogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IGIuR2V0TGVuZ3RoKDApOyBpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgYi5HZXRMZW5ndGgoMSk7IGorKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBiW2ksIGpdID0gcmFuLk5leHQoMSwgMTUpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CgogICAgICAgICAgICBpbnRbLF0gYyA9IG5ldyBpbnRbYS5HZXRMZW5ndGgoMCksIGIuR2V0TGVuZ3RoKDEpXTsKICAgICAgICAgICAgaWYgKGEuR2V0TGVuZ3RoKDEpID09IGIuR2V0TGVuZ3RoKDApKQogICAgICAgICAgICB7CgogICAgICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBhLkdldExlbmd0aCgwKTsgaSsrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgYi5HZXRMZW5ndGgoMSk7IGorKykKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGZvciAoaW50IGsgPSAwOyBrIDwgYi5HZXRMZW5ndGgoMCk7IGsrKykKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgY1tpLCBqXSArPSBhW2ksIGtdICogYltrLCBqXTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKAogICAgICAgICAgICAgICAgICAgICJcbiBOdW1iZXIgb2YgY29sdW1ucyBpbiBGaXJzdCBNYXRyaXggc2hvdWxkIGJlIGVxdWFsIHRvIE51bWJlciBvZiByb3dzIGluIFNlY29uZCBNYXRyaXguIik7CiAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiXG4gUGxlYXNlIHJlLWVudGVyIGNvcnJlY3QgZGltZW5zaW9ucy4iKTsKICAgICAgICAgICAgICAgIEVudmlyb25tZW50LkV4aXQoLTEpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgYy5HZXRMZW5ndGgoMCk7IGkrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBjLkdldExlbmd0aCgxKTsgaisrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGUoIkNbezB9LHsxfV0gOiB7Mn0iLCBpLCBqLCBjW2ksIGpdKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKCkKICAgICAgICB7CiAgICAgICAgICAgIE11bHRpcGx5TWF0cml4KCk7CiAgICAgICAgfQogICAgfQp9
C[0,0] : 87C[0,1] : 42C[0,2] : 117C[0,3] : 61
C[1,0] : 240C[1,1] : 147C[1,2] : 380C[1,3] : 333
C[2,0] : 169C[2,1] : 137C[2,2] : 282C[2,3] : 218
C[3,0] : 127C[3,1] : 53C[3,2] : 174C[3,3] : 101