题目描述: 解题思路: 矩阵为n阶主对角线 mat[i][i]副对角线 i + j == n - 1 代码实现: #include <iostream> using namespace std; const int N = 3; void sum(int mat[N][N], int row, int col) { int dg = 0; int udg = 0; for (int i = 0; i < row; i++) { dg += mat[i][i]; } for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (i + j == row - 1) { udg += mat[i][j]; } } } cout << "first : " << dg << endl; cout << "second : " << udg << endl; } int main() { int mat[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; sum(mat, 3, 3); return 0; }