蛇形数列打印问题

来源:互联网 发布:淘宝怎么登陆不了 编辑:程序博客网 时间:2024/05/22 07:03

2016/04/03做了腾讯的一个蛇形数列打印问题,要求打印出蛇形矩阵
蛇形矩阵的具体要求图形

#include <iostream>#include <cstring>#define MAXN 10using 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;}
0 0
原创粉丝点击