Advertisement
vovanhoangtuan

KTLT - Lesson 2 - 5

Apr 24th, 2020
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Bai5
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int m, n, p;
  14.             int[,] a, b, res;
  15.             string[] temp = Console.ReadLine().Split();
  16.             m = int.Parse(temp[0]);
  17.             n = int.Parse(temp[1]);
  18.             p = int.Parse(temp[2]);
  19.  
  20.             a = new int[m, n];
  21.             b = new int[n, p];
  22.             res = new int[m, p];
  23.             insertMatrix(a);
  24.             insertMatrix(b);
  25.             task(a, b, res);
  26.             showMatrix(res);
  27.         }
  28.  
  29.         static void insertMatrix(int[,] a)
  30.         {
  31.             for (int i = 0; i < a.GetLength(0); i++)
  32.             {
  33.                 string[] temp = Console.ReadLine().Split();
  34.                 for (int j = 0; j < a.GetLength(1); j++)
  35.                 {
  36.                     a[i, j] = int.Parse(temp[j]);
  37.                 }
  38.             }
  39.         }
  40.  
  41.  
  42.         static void task(int[,] a, int[,] b, int[,] res)
  43.         {
  44.             for (int i = 0; i < res.GetLength(0); i++)
  45.             {
  46.                 for (int j = 0; j < res.GetLength(1); j++)
  47.                 {
  48.                     res[i, j] = 0;
  49.                     for (int k = 0; k < res.GetLength(0); k++)
  50.                     {
  51.                         res[i, j] += a[i, k] * b[k, j];
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.  
  57.         static void showMatrix(int[,] res)
  58.         {
  59.             for (int i = 0; i < res.GetLength(0); i++)
  60.             {
  61.                 for (int j = 0; j < res.GetLength(1); j++)
  62.                 {
  63.                     Console.Write($"{res[i, j]} ");
  64.                 }
  65.                 Console.WriteLine();
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement