- HJ提高班第一阶段测试
蛇形2
- 2024-4-10 17:02:43 @
#include using namespace std**;** int main() { int a**[20][20],n;** cin**>>n;** int c**=n,temp=1;//c:当前矩阵边长;temp:当前数字 while(c)** { if(c**==1)//只有一个格 { a[n/2][n/2]=temp;** break; } int y**=(n-c)/2,x=(n-c)/2+c-1;** int end**=y+c-1;** for(;y<=end;y++,temp**++)//处理右面** { a**[y][x]=temp;** } y**=end;** end**=x-c+1;** x**--; for(;x>=end;x--,temp++)//处理下面** { a**[y][x]=temp;** } x**=end;** end**=y-c+1;** y**--; for(;y>=end;y--,temp++)//处理左面** { a**[y][x]=temp;** } y**=end;** end**=x+c-1;** x**++; for(;x<end;x++,temp++)//处理上面** { a**[y][x]=temp;** } c**-=2;//矩阵边长缩小2 } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { cout<<a[i][j]<<' '; } cout<<endl;** } }