C语言实验——求一个3*3矩阵对角线元素之和

C语言实验——求一个3*3矩阵对角线元素之和

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

给定一个3*3的矩阵,请你求出对角线元素之和。

Input

按照行优先顺序输入一个3*3矩阵,每个矩阵元素均为整数。

Output

从左下角到右上角这条对角线上的元素之和

Sample Input

1 2 3
1 2 3
1 2 3

Sample Output

6

Hint

Source

#include<iostream>
#include<cstdio>
using namespace std;
int main(){

    int a[3][3];
    int sum=0;
    for(int i=0;i<=2;i++){
        for(int j=0;j<=2;j++){
            scanf("%d",&a[i][j]);
            if(i+j==2){
                sum+=a[i][j];
            }

        }

    }

    printf("%d\n",sum);
    return 0;


}

注意审题:是左下角到右上角!!!!!!

猜你喜欢

转载自blog.csdn.net/qq_37618760/article/details/80100096