问题 1757: 矩阵对角求和

题目链接:https://www.dotcpp.com/oj/problem1757.html

题目描述

求一个n×n矩阵对角线元素之和,其中1≤n<100,矩阵元素都小于10000。

输入

第一行是一个正整数n。

然后是n行,每行对应矩阵的每行,元素间用空格隔开。

输出

第一行为主对角线元素和,第二行为副对角线的元素和。

样例输入
3
5724 1478 9358
6962 4464 5705
8145 3281 6827
样例输出
17015
21967

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath> 
 4 #include <string>
 5 #include <cstring>
 6 #include <map> 
 7 #include <cstdio>
 8 using namespace std;
 9 int a[105][105]; 
10 int n; 
11 int main()
12 {
13     while(cin>>n){
14         int sum1=0,sum2=0;
15         for(int i=0;i<n;i++){
16             for(int j=0;j<n;j++){
17                 cin>>a[i][j];
18             } 
19         } 
20         for(int i=0;i<n;i++){
21             for(int j=0;j<n;j++){
22                 if(i==j){
23                     sum1+=a[i][j];
24                 }
25                 if(i+j==n-1){
26                     sum2+=a[i][j];
27                 }
28             }
29         }
30         cout<<sum1<<endl;
31         cout<<sum2<<endl;
32     }
33     return 0;
34 }

 

 

posted @ 2019-04-21 17:46  wydxry  阅读(595)  评论(0)    收藏  举报
Live2D