Posted on
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

class Matrix
{
public:
    vector<vector<int>> a;

    Matrix operator+ (const Matrix& rhs)
    {
        vector<vector<int>> res;

        for(int i = 0; i < int(a.size()); i++) {
            const vector<int>& r1 = a[i];
            const vector<int>& r2 = rhs.a[i];
            vector<int> tmp;
            for(auto j = 0; j < int(r1.size()); j++) {
                tmp.push_back(r1[j] + r2[j]);
            }
            res.push_back(tmp);
        }

        return {res};
    }


};

int main () {
   int cases,k;
   cin >> cases;
   for(k=0;k<cases;k++) {
      Matrix x;
      Matrix y;
      Matrix result;
      int n,m,i,j;
      cin >> n >> m;
      for(i=0;i<n;i++) {
         vector<int> b;
         int num;
         for(j=0;j<m;j++) {
            cin >> num;
            b.push_back(num);
         }
         x.a.push_back(b);
      }
      for(i=0;i<n;i++) {
         vector<int> b;
         int num;
         for(j=0;j<m;j++) {
            cin >> num;
            b.push_back(num);
         }
         y.a.push_back(b);
      }
      result = x+y;
      for(i=0;i<n;i++) {
         for(j=0;j<m;j++) {
            cout << result.a[i][j] << " ";
         }
         cout << endl;
      }
   }  
   return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *