蛇形填数

来源:互联网 发布:福建人 知乎 编辑:程序博客网 时间:2024/05/18 17:04
#include <stdio.h>#include<string.h>#include<stdlib.h>/* run this program using the console pauser or add your own getch, system("pause") or input loop *//*代码中学习,日行千里实现蛇形填数:输入4,输出:      10 11 12 1  9  16 13 2  8  15 14 3  7  6  5  4       */ #define  MAXN 13int key[MAXN][MAXN];int main(int argc, char** argv) {  int n; //输入填数的尺寸  scanf("%d",&n);  int total=n*n; int i=1; int x=0,y=n-1; memset(key,0,sizeof(key)); key[x][y]=1;  while(i<total) {  printf("循环开始!%d",i);  system("pause");  while(x+1<n&&!key[x+1][y]) { x++; i++; key[x][y]=i;  } while(y-1>=0&&!key[x][y-1]) { i++; y--; key[x][y]=i; } while(x-1>=0&&!key[x-1][y]) { i++; x--; key[x][y]=i; } while(y+1<n&&!key[x][y+1]) {  i++; y++; key[x][y]=i;  }  }    for(int x=0;x<n;x++)  {    for(int y=0;y<n;y++)  {  printf("%d    ",key[x][y]);  }  printf("\n");  }return 0;}

0 0
原创粉丝点击