top1编程
← 返回题目
题解

【入门】对角线求和

1 条题解

  • 0
    @ 2026-7-28 22:44:49
    #include <bits/stdc++.h>
    using namespace std;
    int a[20][20],n,s;
    int i,j;
    int main() {
    	cin>>n;
    	for(i = 0;i < n;i++){
    		for(j = 0;j < n;j++){
    			cin>>a[i][j];
    		}
    	}
        for(i = 0;i < n;i++){
    		for(j = 0;j < n;j++){
    			if(i==j||i+j==n-1){
                    s=s+a[i][j];
                }
    		}
    	}
    	cout<<s;
    }
    
    • 1