解题思路:
朝一个方向走,到边界就转,走过的标记过了就不走了。
注意事项:
注意输出格式
参考代码
#include<bits/stdc++.h> using namespace std; const int maxn = 103; int a[maxn][maxn]; int main() { int n,x,y,tot=0; while(scanf("%d",&n)==1) { memset(a,0,sizeof(a)); tot = a[x=0][y=n-1]=1; while(tot<n*n) { while(x+1<n&&!a[x+1][y]) a[++x][y] = ++tot; //向下 while(y-1>=0&&!a[x][y-1]) a[x][--y] = ++tot; //left while(x-1>=0&&!a[x-1][y]) a[--x][y] = ++tot; //up while(y+1<n&&!a[x][y+1]) a[x][++y] = ++tot; //right } for(x = 0; x<n;x++) { for(y=0;y<n;y++) if(y==n-1) printf("%d\n", a[x][y]); else printf("%d ", a[x][y]); } putchar('\n'); } return 0; }
0.0分
0 人评分
小明A+B (C语言代码)浏览:1256 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:900 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:867 |
C语言程序设计教程(第三版)课后习题9.8 (C语言代码)浏览:616 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:569 |
DNA (C语言代码)浏览:395 |
C语言程序设计教程(第三版)课后习题6.2 (C语言代码)浏览:541 |
C语言程序设计教程(第三版)课后习题9.3 (C语言代码)浏览:605 |
复数求和 (C语言代码)浏览:929 |
格式化数据输出 (C语言代码)浏览:822 |