#include <stdio.h>void build_matrix(int msize, int a[msize][msize], int size, int value){ int i, row, col; if(size < 1) return; row = col = (msize - size) / 2; if(size==1){ a[row][col] = value; return; } for(i=0;i<size-1;++i) a[row][col++] = value++;//RIGHT for(i=0;i<size-1;++i) a[row++][col] = value++;//DOWN for(i=0;i<size-1;++i) a[row][col--] = value++;//LEFT for(i=0;i<size-1;++i) a[row--][col] = value++;//UP build_matrix(msize, a, size-2, value);}int main(){ int size; printf("input size : "); scanf("%d", &size); int a[size][size]; build_matrix(size, a, size, 1); for(int r=0;r<size;++r){ for(int c=0;c<size;++c) printf("%3d ", a[r][c]); printf("n"); } return 0;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)