#include <stdio.h>
#include<stdlib.h>
int main() {
int a = 0, i = 0, j = 0, c = 0,d = 0,sum = 0,e = 0;
int** b = NULL;
printf("请输入方阵的阶数a:");
scanf_s("%d", &a);
d = a * a;
b = (int **)malloc(d* sizeof(int));
printf("请输入元素,并且每a个元素换一次行。\n");
for (i = 1; i <= a; i++) {
for (j = 1; j <=a; j++) {
scanf_s("%d", &c);
b[i][j] = c;
}
}
for(i = 1 ;i<=a;i++)
{
e = b[i][i];
sum = sum + e;
}
printf("%d", sum);
free(b);
return 0;
}
如果a=3 ,但是只能输入三个数而不是9个