Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<vector<double>> transpose(const vector<vector<double>>& A)
- {
- int m = A.size();
- int n = A[0].size();
- // (m x n) -> (n x m)
- vector<vector<double>> AT(n, vector<double>(m));
- for(int i = 0; i < m; i++)
- {
- for(int j = 0; j < n; j++)
- {
- AT[j][i] = A[i][j];
- }
- }
- return AT;
- }
- vector<vector<double>> multiplyMatrix(const vector<vector<double>>& A, const vector<vector<double>>& B)
- {
- int m = A.size();
- int n = A[0].size();
- int p = B[0].size();
- vector<vector<double>> C(m, vector<double>(p, 0.0));
- for(int i = 0; i < m; i++)
- {
- for(int j = 0; j < p; j++)
- {
- double sum = 0.0;
- for(int k = 0; k < n; k++)
- {
- sum += A[i][k] * B[k][j];
- }
- C[i][j] = sum;
- }
- }
- return C;
- }
- int main()
- {
- int m, n;
- cin >> m >> n;
- vector<vector<double>> A(m, vector<double>(n));
- for(int i = 0; i < m; i++)
- {
- for(int j = 0; j < n; j++)
- {
- cin >> A[i][j];
- }
- }
- // Транспонируем
- auto AT = transpose(A);
- cout << "A^T:\n";
- for(auto& row : AT)
- {
- for(auto& val : row)
- {
- cout << val << " ";
- }
- cout << endl;
- }
- int p;
- cin >> p;
- vector<vector<double>> B(n, vector<double>(p));
- for(int i = 0; i < n; i++)
- {
- for(int j = 0; j < p; j++)
- {
- cin >> B[i][j];
- }
- }
- auto C = multiplyMatrix(A, B);
- cout << "A x B:\n";
- for(auto& row : C)
- {
- for(auto& val : row)
- {
- cout << val << " ";
- }
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement