題目33 蛇形填數
阿新 • • 發佈:2019-02-15
已AC程式碼:
#include<cstdio> #include<cstring> using namespace std; int main() { int n; while(scanf("%d", &n) != EOF) { int a[n][n], top=1, x, y; x = -1; y = n-1; memset(a, 0, sizeof(a)); while(top <= n*n) { while(x<n-1 && !a[x+1][y]) a[++x][y] = top++; while(y>0 && !a[x][y-1]) a[x][--y] = top++; while(x>0 && !a[x-1][y]) a[--x][y] = top++; while(y<n && !a[x][y+1]) a[x][++y] = top++; } for(int i=0; i<n; i++) { for(int j=0; j<n; j++) printf("%d ", a[i][j]); printf("\n"); } } return 0; }