在本教程中,我们将讨论一个程序,该程序查找通过添加相同矩阵的行优先级和列优先级顺序形成的矩阵轨迹。
为此,将为我们提供两个数组,一个在行大中,另一个在列大中。我们的任务是找到通过将两个给定矩阵相加而形成的矩阵的轨迹。
#include <bits/stdc++.h> using namespace std; //计算新矩阵的calculateMatrixTrace- int calculateMatrixTrace(int row, int column) { int A[row][column], B[row][column], C[row][column]; int count = 1; for (int i = 0; i < row; i++) for (int j = 0; j < column; j++) { A[i][j] = count; count++; } count = 1; for (int i = 0; i < row; i++) for (int j = 0; j < column; j++) { B[j][i] = count; count++; } for (int i = 0; i < row; i++) for (int j = 0; j < column; j++) C[i][j] = A[i][j] + B[i][j]; int sum = 0; for (int i = 0; i < row; i++) for (int j = 0; j < column; j++) if (i == j) sum += C[i][j]; return sum; } int main() { int ROW = 6, COLUMN = 9; cout << calculateMatrixTrace(ROW, COLUMN) << endl; return 0; }
输出结果
384