首页 > 代码库 > 蛇形填数

蛇形填数

好吧,我承认题目不难,只是我遗忘的太多了;正在一点点的找回来。这个版本的蛇形填数是从(0,0)开始横着填的,

#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int N=12;int main(){int oi[N][N];    int n;    while(cin>>n)    {if(n>N) break;        memset(oi,0,sizeof(oi));   int  count=1;    int i=0,j=0;while(count<n*n){    while(j<n-1&&!oi[i][j+1]){oi[i][j++]=count++;}    while(i<n-1&&!oi[i+1][j]){oi[i++][j]=count++;}    while(j>=1&&!oi[i][j-1]){oi[i][j--]=count++;}    while(i>=1&&!oi[i-1][j]){oi[i--][j]=count++;}  }oi[i][j]=count;for(i=0;i<n;i++)        {for(j=0;j<n;j++)   printf("%3d\t",oi[i][j]);    cout<<endl;}    }}

  这个是从(0,n-1)开始的

#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int N=12;int main(){int oi[N][N];    int n;    while(cin>>n)    {if(n>N) break;        memset(oi,0,sizeof(oi));   int  count=1;    int i=0,j=n-1;while(count<n*n){    while(i<n-1&&!oi[i+1][j]){oi[i++][j]=count++;}        while(j>=1&&!oi[i][j-1]){oi[i][j--]=count++;}    while(i>=1&&!oi[i-1][j]){oi[i--][j]=count++;}    while(j<n-1&&!oi[i][j+1]){oi[i][j++]=count++;}}oi[i][j]=count;for(i=0;i<n;i++)        {for(j=0;j<n;j++)   printf("%3d\t",oi[i][j]);    cout<<endl;}    }}