AcWing 753. 平方矩阵 I
原题链接
困难
作者:
满地王八我壳最绿.
,
2023-10-29 17:03:19
,
所有人可见
,
阅读 40
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
const int N=110;
int arr[N][N];
while(true)//不知道循环的次数,运用while(true)是循环在达到break的条件下结束循环
{
cin>>n;
if(n==0)
break;
int top=0,bottom=n-1,left=0,right=n-1;//定义上下左右四个边界
int count=0;
while(top<=bottom&&left<=right) //只要在边界内,一直循环
{
count++; //定义一个计数器,方便计数
for(int i=left;i<=right;i++) //从左向右
{
arr[top][i]=count;
}
top++;
for(int i=top;i<=bottom;i++) //从右向下
{
arr[i][right]=count;
}
right--;
for(int i=right;i>=left;i--) //从右向左
{
arr[bottom][i]=count;
}
bottom--;
for(int i=bottom;i>=top;i--) //从下向上
{
arr[i][left]=count;
}
left++;
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
return 0;
}