AcWing 754 square matrix

Title description:

Enter an integer N, and output a two-dimensional array of order N.

Refer to the example for the format of the array.

Input format

The input contains multiple lines, and each line contains an integer N.

When the input line N=0, it means the input is over, and the line does not need to be processed.

Output format

For each input integer N, output a two-dimensional array of order N that meets the requirements.

Each array occupies N rows, and each row contains N integers separated by spaces.

After each array is output, a blank line is output.

data range

0≤N≤1000≤N≤100

Input sample:

1
2
3
4
5
0

Sample output:

1

1 2
2 1

1 2 3
2 1 2
3 2 1

1 2 3 4
2 1 2 3
3 2 1 2
4 3 2 1

1 2 3 4 5
2 1 2 3 4
3 2 1 2 3
4 3 2 1 2
5 4 3 2 1

 Observing the sample, it is found that the numbers on the diagonal diagonal from left to right are symmetrical, and the diagonal from the left diagonal to the upper right is 1, 2, 3,. . . Wait

#include <iostream>
#include <cstdio>

using namespace std;
const int MAX = 109;

int n;
int a[MAX][MAX];

int main()
{
    while(scanf("%d", &n))
    {
        if(n == 0)
            break;

        int m = n - 1;

        for(int i = 0; i < n; i++)
            a[i][i] = 1;

        int cou = 2;

        while(m -- )
        {
            for(int i = 0; i < n - cou + 1; i ++)
            {
                int row = i + cou - 1;
                int col = i;
                a[row][col] = a[col][row] = cou;
                //printf("row = %d  col = %d  cou = %d a = %d\n", row, col, cou, a[row][col]);
            }
            cou++;
        }

        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
                printf("%d ", a[i][j]);
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44620183/article/details/113441531