蛇形填数

来源:互联网 发布:英雄联盟皮肤淘宝店 编辑:程序博客网 时间:2024/05/05 08:01

#include <iostream>using namespace std;#define  MAX  10int  a[MAX][MAX];int  main(){int n,x,y,tot=0,i,j;cin>>n;memset(a,0,sizeof(a));tot=a[x=0][y=n-1]=1;while(tot<n*n){while(x+1<n  && !a[x+1][y]) a[++x][y]=++tot;//表示↘while(y-1>=0 && !a[x][y-1]) a[x][--y]=++tot;//表示下左;while(x-1>=0 && !a[x-1][y]) a[--x][y]=++tot;//表示↖;while(y+1<n  && !a[x][y+1]) a[x][++y]=++tot;//表示上右}for(i=0;i<n;i++){for(j=0;j<n-1;j++)cout<<a[i][j]<<"    ";cout<<a[i][j]<<endl;}return 0;  }


原创粉丝点击