问题及代码
Description
求一个3×3矩阵对角线元素之和。
Input
矩阵
Output
主对角线 副对角线 元素和
Sample Input
1 2 3
1 1 1
3 2 1
Sample Output
3 7
/*烟台大学计算机学院 2016
作者: 马春澎
完成日期:2016年12月1日 */
#include<stdio.h>
int main()
{
int i,j,b,c;
int a[3][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
b=c=0;
for(i=0;i<3;i++)
{
b=b+a[i][i];
c=c+a[i][2-i];
}
printf("%d %d",b,c);
return 0;
}
运算结果
知识点总结
二维数组的应用
学习心得
要明白每个数组所在的位置,找到规律再进行运算.