Advertisement
Solingen

z15.2.cpp

Dec 22nd, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<vector<double>> transpose(const vector<vector<double>>& A)
  6. {
  7.     int m = A.size();
  8.     int n = A[0].size();
  9.     // (m x n) -> (n x m)
  10.     vector<vector<double>> AT(n, vector<double>(m));
  11.     for(int i = 0; i < m; i++)
  12.     {
  13.         for(int j = 0; j < n; j++)
  14.         {
  15.             AT[j][i] = A[i][j];
  16.         }
  17.     }
  18.     return AT;
  19. }
  20.  
  21. vector<vector<double>> multiplyMatrix(const vector<vector<double>>& A, const vector<vector<double>>& B)
  22. {
  23.     int m = A.size();
  24.     int n = A[0].size();
  25.     int p = B[0].size();
  26.     vector<vector<double>> C(m, vector<double>(p, 0.0));
  27.  
  28.     for(int i = 0; i < m; i++)
  29.     {
  30.         for(int j = 0; j < p; j++)
  31.         {
  32.             double sum = 0.0;
  33.             for(int k = 0; k < n; k++)
  34.             {
  35.                 sum += A[i][k] * B[k][j];
  36.             }
  37.             C[i][j] = sum;
  38.         }
  39.     }
  40.     return C;
  41. }
  42.  
  43. int main()
  44. {
  45.     int m, n;
  46.     cin >> m >> n;
  47.     vector<vector<double>> A(m, vector<double>(n));
  48.     for(int i = 0; i < m; i++)
  49.     {
  50.         for(int j = 0; j < n; j++)
  51.         {
  52.             cin >> A[i][j];
  53.         }
  54.     }
  55.  
  56.     // Транспонируем
  57.     auto AT = transpose(A);
  58.     cout << "A^T:\n";
  59.     for(auto& row : AT)
  60.     {
  61.         for(auto& val : row)
  62.         {
  63.             cout << val << " ";
  64.         }
  65.         cout << endl;
  66.     }
  67.  
  68.     int p;
  69.     cin >> p;
  70.     vector<vector<double>> B(n, vector<double>(p));
  71.     for(int i = 0; i < n; i++)
  72.     {
  73.         for(int j = 0; j < p; j++)
  74.         {
  75.             cin >> B[i][j];
  76.         }
  77.     }
  78.  
  79.     auto C = multiplyMatrix(A, B);
  80.     cout << "A x B:\n";
  81.     for(auto& row : C)
  82.     {
  83.         for(auto& val : row)
  84.         {
  85.             cout << val << " ";
  86.         }
  87.         cout << endl;
  88.     }
  89.  
  90.     return 0;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement