Blue Bridge Cup Matrix Addition Simulation

Problem Description
  Given two N × M matrices, calculate the sum. Among them:
  N and M are greater than or equal to 1 and less than or equal to 100, and the absolute value of matrix elements does not exceed 1000.
Input format
  The first row of the input data contains two integers N and M, indicating the number of rows and columns of the two matrices to be added. The next 2 * N rows each contain M numbers, where the first N rows represent the first matrix and the last N rows represent the second matrix.
Output format
  Your program needs to output an N * M matrix, representing the result of the addition of the two matrices. Note that there should be no extra spaces at the end of each line in the output, otherwise your program may be considered as Presentation Error by the system
Sample input
2 2
1 2
3 4
5 6
7 8

Sample output

6 8
10 12 
Do this question after doing the matrix fast power. . I wrote a three-layer for loop at the beginning, and then how to change it is wrong. .
Sorry for my linear algebra teacher, I confuse matrix multiplication and addition.
This question is directly added to the two-layer for loop directly.
Shameful WA twice.
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int a[110][110];
 4 int b[110][110];
 5 int main() {
 6     int n, m;
 7     cin >> n >> m;
 8     for (int i = 1; i <= n; i++) {
 9         for (int j = 1; j <= m; j++) {
10             cin >> a[i][j];
11         }
12     }
13     for (int i = 1; i <= n; i++) {
14         for (int j = 1; j <= m; j++) {
15             cin >> b[i][j];
16         }
17     }
18     for (int i = 1; i <= n; i++) {
19         for (int j = 1; j <= m; j++) {
20             a[i][j] += b[i][j];
21         }
22     }
23     for (int i = 1; i <= n; i++) {
24         for (int j = 1; j <= m; j++) {
25             if (j != m) {
26                 cout << a[i][j] << " ";
27             } else {
28                 cout << a[i][j] << endl;
29             }
30         }
31     }
32     return 0;
33 }


Guess you like

Origin www.cnblogs.com/fx1998/p/12691175.html