'commit'
This commit is contained in:
68
TangDou/AcWingSuanFaJinJieZhiNan/MatrixAddDec.cpp
Normal file
68
TangDou/AcWingSuanFaJinJieZhiNan/MatrixAddDec.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
typedef long long LL;
|
||||
const int N = 110;
|
||||
int n = 3;
|
||||
|
||||
//两个n*n的矩阵
|
||||
/*
|
||||
z[][N]:结果数组
|
||||
C++中,如果传递的函数参数是数组,则理解为按地址传递,
|
||||
在函数内的修改动作将直接修改数组内容,调用者可以直接
|
||||
使用修改后的内容。
|
||||
不论是几维的多维数组,只有第一维的长度不用指定,其它都要指定。
|
||||
黄海理解一下:
|
||||
因为多维数组在内存中是连续存的,C++需要知道你给的这个指针,到哪个位置开始换行。
|
||||
*/
|
||||
void add(LL z[][N], LL x[][N], LL y[][N]) {
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
z[i][j] = x[i][j] + y[i][j];
|
||||
}
|
||||
void dec(LL z[][N], LL x[][N], LL y[][N]) {
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
z[i][j] = x[i][j] - y[i][j];
|
||||
}
|
||||
/*
|
||||
测试用例
|
||||
1 2 3
|
||||
1 2 3
|
||||
1 2 3
|
||||
|
||||
4 5 6
|
||||
4 5 6
|
||||
4 5 6
|
||||
|
||||
输出结果:
|
||||
5 7 9
|
||||
5 7 9
|
||||
5 7 9
|
||||
*/
|
||||
|
||||
LL x[N][N], y[N][N], z[N][N];
|
||||
int main() {
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
cin >> x[i][j];
|
||||
|
||||
for (int i = 1; i <= n; i++)
|
||||
for (int j = 1; j <= n; j++)
|
||||
cin >> y[i][j];
|
||||
|
||||
add(z, x, y);
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
cout << z[i][j] << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
dec(z, x, y);
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++)
|
||||
cout << z[i][j] << " ";
|
||||
cout << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user