Today's emotion: Y's general idea is really ingenious, and you will know it as soon as you hear it!
The topic comes from the Acwing Grammar Fundamentals Course
753. Input an integer N, and output a 2D array of order N in the shape of a glyph.
The outermost level of the array is 1, the next outer level is 22, and so on.
input format
The input consists of multiple lines, each line containing an integer N.
When the input line N=0, it means that the input is over, and the line does not need to be processed.
output format
For each input integer N, output a 2D array of order N that satisfies the requirement.
Each array occupies N lines, and each line contains N integers separated by spaces.
After each array is output, a blank line is output.
data range
0≤N≤100
input sample
Topic Analysis:
Each circle is a layer of shape, so let's make the outermost layer have a shape (understood as a boundary, but not marked) , take any point on the shape as a vertical line in the up, down, left, and right directions, and meet the boundaries at four points, The minimum value of these four vertical lines is the number that should be filled in row i and column j.
The breakthrough lies in constructing a vertical line based on the characteristics of the loop shape, and it is easy to know what number to add to this point through the size of the vertical line.
AC code
#include <iostream>
using namespace std;
int main()
{
int n;
while (cin>>n,n)
{
for (int i=0;i<n;i++)
{
for (int j = 0;j<n;j++)
{
int left=j+1,right=n-j,up=i+1,down=n-i;
cout<<min(min(left,right),min(up,down))<<" ";
}
cout<<endl;
}
}
return 0;
}