请高手看看这些数的规律,用程序怎样写
1 2 6 7 153 5 8 14 16
4 9 13 17 22
10 12 18 21 23
11 19 20 24 25
#include <stdio.h> int main(void) { int i,j,k,n,m,a[50][50]; printf("n="); scanf("%d",&n); for(k=1,m=i=j=0;k<=n*n;k++) { a[i][j]=k; if(m%2==0) /* 第一行m=0,m能整除2是奇数行 */ { if(j+1!=n) if(i==0)j++,m++; else j++,i--; else i++,m++; } else /* 否则偶数行 */ { if(i+1!=n) { if(j==0)i++,m++; else i++,j--; } else j++,m++; } } for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%3d",a[i][j]); printf("\n\n"); } printf("\n\n"); return 0; }
蛇形矩阵­ 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7 #include <stdio.h>­ ­ int main()­ {­ int n; ­ int dx[4] = {-1,0,1,0}, dy[4] = {0,1,0,-1}; // 北0 东1 南2 西3 // 相当一个循环队列­ int dx1[4] = {0,1,0,1}, dy1[4] = {1,0,-1,0}; // 东南西南­ while(scanf("%d", &n) != EOF) ­ {­ int map[32][32] = {0}; ­ int x, y, d, nStep = 2;­ ­ for(x = 0, y = n + 1; x < n+2; x++) ­ map[x][0] = map[x][y] = -1;­ for(x = 0, y = n + 1; x < n+2; x++) ­ map[0][x] = map[y][x] = -1;­ ­ x = y = 1; ­ d = 1; ­ ­ for(map[1][1] = 1; nStep < 4*n-3; nStep++) ­ {­ x += dx[d]; y += dy[d];­ map[x][y] = nStep;­ if (map[x+dx[d]][y+dy[d]]) ­ d = (d+1) % 4; ­ } ­ ­ d = 0;­ for(; nStep < n*n+1; nStep++) ­ {­ x += dx1[d]; y += dy1[d];­ map[x][y] = nStep;­ if (map[x+dx1[d]][y+dy1[d]] || d == 1 || d == 3) ­ d = (d+1) % 4; ­ ­ } ­ printf("\n");­ for(x = 1; x < n+1; x++) ­ {­ for(y = 1; y < n+1; y++)­ printf("%4d",map[x][y]);­ printf("\n");­ }­ printf("\n");­ }­ return 0;­ }­