1 条题解

  • 0
    @ 2023-6-11 12:20:59

    C :

    #include <stdio.h>
    
    void main(){
    	int a[10][10];
    	int i,n,j,x=0;
    	scanf("%d",&n);
    
    	for(i=0;i<n;i++){
    		for(j=0;j<n;j++){
    			scanf("%d",&a[i][j]);	
    		}
    	}
    
    	for(i=0;i<n;i++){
    		x=x+a[i][i]+a[i][n-i-1];
    	}
    
    	printf("%d",x);
    }
    

    C++ :

    #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++){
    		//05 14 23 32 41 50
    		s = s + a[i][i];
    		s = s + a[i][n-i-1];
    	}
    	
    	cout<<s;
    }
    
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int s = 0 ;
    		int[][] a = new int[n][n];
    		for (int i = 0; i < n; i++) {
    			for (int j = 0; j < n; j++) {
    				a[i][j] = sc.nextInt();
    			}
    		}
    		for (int i = 0; i < n; i++) {
    				s = s + a[i][i];
    				s = s + a[i][n-i-1];
    		}
    		System.out.println(s);
    
    	}
    }
    
    • 1

    信息

    ID
    2664
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者