C language: write a program to realize the following regular 5*5 matrix into an array, and output the array

Topic source: Dagong MOOC Link
Author: Caleb Sung

Topic requirements

Write a program to store the following regular 5*5 matrix into an array, and output the array

1       1       1       1       1
1       2       2       2       1
1       2       3       2       1
1       2       2       2       1
1       1       1       1       1

ideas

After observation, the value of each element in the square matrix is ​​the smallest of the following four attribute values:

  • Number of lines
  • number of columns
  • 6-number of lines
  • 6 - number of columns

From this, the code can be written.

Reference solution

Because I am really worried about confusing myself, here I define a 6*6 matrix with int, and 0 rows and 0 columns are not used:

#include<stdio.h>
void main()
{
    int a[6][6], i, j, tmp;

    //赋值 
    for(i=1; i<=5; i++){
        for(j=1; j<=5; j++){
            tmp = 99;
            if(i < tmp)
                tmp = i;
            if(j < tmp)
                tmp = j;
            if(6-i < tmp)
                tmp = 6-i;
            if(6-j < tmp)
                tmp = 6-j;
            a[i][j] = tmp;
        }
    }

    //打印
    for(i=1; i<=5; i++)
        for(j=1; j<=5; j++)
        {
            printf("%d\t",a[i][j]);
            if(j==5)
                printf("\n");
        }
    printf("\n");

} 

operation result

1       1       1       1       1
1       2       2       2       1
1       2       3       2       1
1       2       2       2       1
1       1       1       1       1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324490041&siteId=291194637