#include <stdio.h> #include <stdlib.h> #include <math.h> #define MAX 5 int main() { int a[MAX][MAX] = { 0 }; int total = 0; //初始化数据 for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX;j++) { a[i][j] = rand() % 100; } } //显示数据 for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { printf("%5d ", a[i][j]); } printf(" "); } //获取矩阵的对角值和 for (int i = 0; i < MAX; i++) { total += a[i][i]; if (i!=MAX-1-i) total += a[i][MAX-1-i]; } printf("%d", total); system("pause"); return 0; }