首页 > 代码库 > 输入一个正整数n (1<n<=10),生成 1个 n*n 方阵 求出对角线之和

输入一个正整数n (1<n<=10),生成 1个 n*n 方阵 求出对角线之和

#define  _CRT_SECURE_NO_WARNINGS#include <Windows.h>#include <stdio.h>#include <stdlib.h>void main() {    int n = 0;    scanf("%d", &n);    int a[50][50] = { 0 };    int result = 0;    for (int i = 0; i < n * n; i++) {        a[i / n][i % n] = i + 1;        if ((i % n) == 0){            result += a[i / n][i % n] + (i / n);        }    }    for (int i = 0; i < n; i++) {        for (int j = 0; j < n; j++)        {            printf("%-6d", a[i][j]);        }        printf("\n");    }    printf("result=%d", result);    getchar();    getchar();}

输入一个正整数n (1<n<=10),生成 1个 n*n 方阵 求出对角线之和