蛇形数列打印问题
2016-04-03 22:50
267 查看
2016/04/03做了腾讯的一个蛇形数列打印问题,要求打印出蛇形矩阵
![](https://oscdn.geek-share.com/Uploads/Images/Content/202010/31/47ff513e6c0eca8c83ccf3435d4e0d92)
#include <iostream> #include <cstring> #define MAXN 10 using namespace std; int a[MAXN][MAXN]; int main() { int n,x,y,t=0; cin>>n; memset(a,0,sizeof(a)); x=0; y=0; t=a[x][y]=1; while (t<n*n) { while (y<n-1 && !a[x][y+1]) a[x][++y]=++t; while (x<n-1 && !a[x+1][y]) a[++x][y]=++t; while (y>=1 && !a[x][y-1]) a[x][--y]=++t; while (x>=1 && !a[x-1][y]) a[--x][y]=++t; } for (x=0;x<n;x++) { int i=0; for(y=0;y<n;y++) { i++; if (i!=n) cout<<a[x][y]<<"\t"; else cout<<a[x][y]<<endl; } } return 0; }