L
L
levykkn2021-11-03 03:10:48
C++ / C#
levykkn, 2021-11-03 03:10:48

How to do what would be like in the example?

#include "stdio.h"
#define N 100

int main() {
    int A[N][N];
    int k = 1, j, i, p, n;
    printf("Введите количество столбцов и строк: ");
    scanf("%d", &n);
    
    p = n / 2;
    
    for (i = 1; i <= p; i++) {
        for (j = n-i; j>=i ; j--) {
            A[j][n - i] = k++;
        }
        for(j = n-i; j>=i; j--) { 
            A[i - 1][j] = k++;
        }
        for(j = n-i+1; j>=i;j--) {
            A[n - j][i - 1] = k++;
        }
        for(j = n-i; j > i; j--) { 
            A[n - i][n - j] = k++;
        } 
    }
    
    if (n % 2 == 1) {
        A[p][p] = n * n;
    }
    
    for (k = 0; k < n; k++)
        for (j = 0; j < n; j++) {
            printf(" %4d ", A[k][j]);
            if (j == n - 1) printf("\n");
        }
}

Outputs:
6181d3033b093207292872.png
And should:
6181d3476c49c391690934.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question