下面是一个5阶的螺旋方阵,具体情况如下:

试编程打印出此形式的n(n<10)阶的方阵(顺时针方向旋进).
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

呼啦一阵风
浏览 271回答 2
2回答

FFIVE

// It is so easy. Just use five FOR sentences with high efficiency and simplest.void fun(int **p, unsigned char n) // create a screw matrix with n{ // return by pointer punsigned char k, i, l = (n+1)/2;int c = 1;p[l-1][l-1] = n * n;for(k=0; k<l; k++, n-=2){for(i=0;i<n-1;i++) p[k][k+i]=c++;for(i=0;i<n-1;i++) p[k+i][k+n-1]=c++;for(i=n-1;i>0;i--) p[k+n-1][k+i]=c++;for(i=n-1;i>0;i--) p[k+i][k]=c++;}} // end of fun// You can write a main function to test it, which is so simple even without using one IF sentence.

幕布斯7119047

#include <stdio.h>//Up,Down,Left,Right#define U 'U'#define D 'D'#define L 'L'#define R 'R'const int MAX = 10;int main(int argc, char *argv[]){&nbsp; &nbsp; int matrix[MAX][MAX];&nbsp; &nbsp; int n, x, y, k;&nbsp; &nbsp; char direction; //Up,Down,Left,Right&nbsp; &nbsp; // get input&nbsp; &nbsp; printf("Please input n(n<%d): ", MAX+1);&nbsp; &nbsp; scanf("%d", &n);&nbsp; &nbsp; // invalid input&nbsp; &nbsp; if(n>MAX || n<1) return -1;&nbsp; &nbsp; // initializations&nbsp; &nbsp; for (y=0;y<n;y++)&nbsp; &nbsp; &nbsp; &nbsp; for (x=0;x<n;x++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrix[x][y]=0;&nbsp; &nbsp; for (y=n;y<MAX;y++)&nbsp; &nbsp; &nbsp; &nbsp; for (x=n;x<MAX;x++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrix[x][y]=-1;&nbsp; &nbsp; x=y=k=0;&nbsp; &nbsp; direction = R;&nbsp; &nbsp; // build the matrix&nbsp; &nbsp; while(1){&nbsp; &nbsp; &nbsp; &nbsp; if (matrix[x][y]==0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrix[x][y]=++k;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (direction){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case U:y--;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case D:y++;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case L:x--;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R:x++;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (direction){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case U:direction=R;y++;x++;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case D:direction=L;y--;x--;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case L:direction=U;y--;x++;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R:direction=D;y++;x--;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // finished&nbsp; &nbsp; &nbsp; &nbsp; if (k==n*n) break;&nbsp; &nbsp; }&nbsp; &nbsp; // output&nbsp; &nbsp; for (y=0;y<n;y++){&nbsp; &nbsp; &nbsp; &nbsp; for (x=0;x<n;x++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("%3d", matrix[x][y]);&nbsp; &nbsp; &nbsp; &nbsp; printf("\n");&nbsp; &nbsp; }&nbsp; &nbsp; // exit with no error&nbsp; &nbsp; return 0;}-----------------------------------------------------
打开App,查看更多内容
随时随地看视频慕课网APP