Advertisement
Gornak40

Multiply idz 2

Oct 21st, 2022
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <vector>
  2. #include <cstddef>
  3. #include <iostream>
  4.  
  5. using Matrix = std::vector<std::vector<long long>>;
  6.  
  7. Matrix Multiply(const Matrix& A, const Matrix& B) {
  8.     size_t n = A.size();
  9.     size_t m = B[0].size();
  10.     size_t q = A[0].size();
  11.     Matrix C(n, std::vector<long long>(m, 0));
  12.     for (size_t i = 0; i < n; ++i) {
  13.         for (size_t j = 0; j < m; ++j) {
  14.             for (size_t k = 0; k < q; ++k) {
  15.                 C[i][j] += A[i][k] * B[k][j];
  16.             }
  17.         }
  18.     }
  19.     return C;
  20. }
  21.  
  22. void Print(const Matrix& C) {
  23.     for (const auto& line: C) {
  24.         for (const auto& x: line) {
  25.             std::cout << x << ' ';
  26.         }
  27.         std::cout << std::endl;
  28.     }
  29. }
  30.  
  31. int main() {
  32.     Matrix A = {
  33.         {1   , 0    ,-1,     0},
  34.     {0,   1,    -4, -4},
  35.     {1    ,2    ,-9 ,-8},
  36.     {1  ,-12,   47, 48},
  37.     };
  38.     Matrix B = {
  39.         {4  ,-1,-2  ,-1},
  40.     {3  , 1 ,-1 ,-1},
  41.    {-2  , 2  ,2  ,1},
  42.    {-2  , 0 , 1  ,1},
  43.     };
  44.     Print(Multiply(A, B));
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement