-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
12 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,27 @@ | ||
ปัญหาที่ให้มาในข้อนี้คือการบวกเมทริกซ์ขนาด m x n ที่ให้มาในข้อมูลนำเข้า $A$ และ $B$ จริงๆแล้วเราสามารถแทนได้ด้วย 2d array ดังนั้นเราสามารถที่จะไล่ for loop สองชั้นโดยในแต่ละขั้นตอนเราจะมีคู่อันดับ $(i, j)$ บ่งบอกถึงหนึ่งช่องของเมทริกซ์ ดังนั้นเราสามารถจะ print $A[i][j] + B[i][j]$ ออกมาได้เลย โค้ดของโจทย์ข้อนี้เป็นดังต่อไปนี้ | ||
ปัญหาที่ให้มาในข้อนี้คือการบวกเมทริกซ์ขนาด $m \times n$ ที่ให้มาในข้อมูลนำเข้า $A$ และ $B$ จริง ๆ แล้วเราสามารถแทนได้ด้วย 2D array ดังนั้นเราสามารถที่จะไล่ for loop สองชั้นโดยในแต่ละขั้นตอนเราจะมีคู่อันดับ $(i, j)$ บ่งบอกถึงหนึ่งช่องของเมทริกซ์ ดังนั้นเราสามารถจะ แสดงค่า $A[i][j] + B[i][j]$ ออกมาได้เลย โค้ดของโจทย์ข้อนี้เป็นดังต่อไปนี้ | ||
|
||
```cpp | ||
#include <cstdio> | ||
using namespace std; | ||
|
||
int m, n, A[100][100], B[100][100]; | ||
int a[110][110], b[110][110]; | ||
int main() { | ||
int m, n; | ||
scanf("%d %d", &m, &n); | ||
for(int i = 0; i < m; i++) { | ||
for(int j = 0; j < m; j++) { | ||
scanf("%d", &A[i][j]); | ||
for (int i = 1; i <= m; ++i) { | ||
for (int j = 1; j <= n; ++j) { | ||
scanf("%d", &a[i][j]); | ||
} | ||
} | ||
for(int i = 0; i < m; i++) { | ||
for(int j = 0; j < m; j++) { | ||
scanf("%d", &B[i][j]); | ||
for (int i = 1; i <= m; ++i) { | ||
for (int j = 1; j <= n; ++j) { | ||
scanf("%d", &b[i][j]); | ||
} | ||
} | ||
for(int i = 0; i < m; i++) { | ||
for(int j = 0; j < m; j++) { | ||
printf("%d ", A[i][j] + B[i][j]); | ||
for (int i = 1; i <= m; ++i) { | ||
for (int j = 1; j <= n; ++j) { | ||
printf("%d ", a[i][j] + b[i][j]); | ||
} | ||
printf("\n"); | ||
} | ||
return 0; | ||
} | ||
``` | ||
|