A1033. 绘制图形
时间限制:
1.0s 内存限制:
512.0MB
问题描述
找出下面给出图形的规律,给出n,画一个n行的对应图形。
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
输入格式
输入包含一个数n。1<=n<=30
输出格式
输出与上图类似n行的图形。
样例输入
4
样例输出
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
12 13 14 5
11 16 15 6
10 9 8 7
- #include <stdio.h>
- #include <stdlib.h>
- int main(){
- int n;
- while((scanf("%d",&n))!=EOF){
- int x[31][31]={0};
- int l=1,h=1,count=1;
- x[1][1]=1;
- while(count<n*n){
- while(l<n&&x[h][l+1]==0){x[h][++l]=++count;}
- while(h<n&&x[h+1][l]==0){x[++h][l]=++count;}
- while(l-1>0&&x[h][l-1]==0){x[h][--l]=++count;}
- while(h>0&&x[h-1][l]==0){x[--h][l]=++count;}
- }
- for(h=1;h<=n;h++){
- for(l=1;l<=n;l++){
- printf("%d ",x[h][l]);
- }
- printf("\n");
- }
- }
- system("pause");
- return 0;
- }