题解

34 条题解

  • 2
    @ 2018-03-25 03:00:17

    一居首行正中央,
    斜上方向填数忙,
    出上移下,
    出右移左,
    双出占位写下方。

    小学奥数的口诀。。。

  • 2
    @ 2017-11-04 10:00:52

    简单到爆的土豆法
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int n,i,j,a[1010][1010],c,x,k,m;
    int main()
    {

    cin>>n;
    a[1][n/2+1]=1;
    k=1;
    x=1;
    c=n/2+1;
    m=n;
    n=n*n-1;
    while(n>0)
    {
    n--;
    k++;
    if(x==1&&c!=m)
    {
    x=m;c++;
    }
    else if(x!=1&&c==m)
    {
    x--;c=1;
    }
    else if(x==1&&c==m)
    x++;
    else if(x!=1&&c!=m&&a[x-1][c+1]==0)
    {
    x--;c++;
    }
    else if(x!=1&&c!=m&&a[x-1][c+1]!=0)
    x++;
    a[x][c]=k;
    }
    for(i=1;i<=m;i++)
    {
    for(j=1;j<=m;j++)
    cout<<a[i][j]<<" ";
    cout<<endl;
    }
    return 0;
    }

  • 1
    @ 2021-09-04 14:05:55
    #include <bits/stdc++.h>
    using namespace std;
    
    int n,a[40][40],x,y; 
    int main(){
        cin>>n;
        x=1,y=(n+1)/2;
        for(int i=1; i<=n*n; i++){
            a[x][y]=i;
            if(!a[(x-2+n)%n+1][y%n+1])
                x=(x-2+n)%n+1,y=y%n+1;
            else
                x=x%n+1;
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
                cout<<a[i][j]<<" ";
            cout<<"\n";
        }
        return 0;
    }
    
  • 1
    @ 2017-11-07 17:12:37
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int N,a,b,c,k,i,j;
    int hf[40][40];
    int main() {
    //  fleopen("a1.txt","r",stdin);
    //  fleopen("b1.txt","w",stdout);
        cin>>N;
        a=1;
        b=(N+1)/2;
        hf[a][b]=1;
        for(k=2; k<=N*N; k++) {
            if(a==1&&b!=N) {
                a=N;
                b=b+1;
                hf[a][b]=k;
            } else if(b==N&&a!=1) {
                a=a-1;
                b=1;
                hf[a][b]=k;
            } else if(a==1&&b==N) {
                a=a+1;
                hf[a][b]=k;
            } else if(a!=1&&b!=N) {
                if (hf[a-1][b+1]==0) {
                    a=a-1;
                    b=b+1;
                    hf[a][b]=k;
                } else {
                    a=a+1;
                    hf[a][b]=k;
                }
            }
        }
        for( int a=1; a<=N; a++) {
            for(int b=1; b<=N; b++)
                cout<<hf[a][b]<<" ";
            cout<<endl;
        }
    //  fclose(stdin); fclose(stdout);
        return 0;
    }
    
  • 1
    @ 2017-10-23 16:57:50
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    #include <cctype>
    #include <vector>
    #include <queue>
    const int MaxN=1000;
    using namespace std;
    int a[MaxN][MaxN];
    int main()
    {
        int N,k=1,l;
        scanf("%d",&N);
        memset(a,0,sizeof(a));
        a[1][(N+1)/2]=1;
        l=(N+1)/2;
        for(int j=2;j<=(N)*(N);j++)
        {
            if((k==1&&l==N)||a[k-1][l+1])
            {
                a[++k][l]=j;
                continue;
            }
            if(k==1)
            {
                a[N][++l]=j;
                k=N;
                continue;
            }
            if(l==N)
            {
                a[--k][1]=j;
                l=1;
                continue;
            }
            a[--k][++l]=j;
        }
        for(int i=1;i<=N;++i)
        {
            for(int j=1;j<N;++j)
            {
                printf("%d ",a[i][j]);
            }
            printf("%d\n",a[i][N]);
        }
        return 0;
    }
    
  • 1
    @ 2016-11-05 19:43:03

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int a[100][100];
    int main()
    {
    int n;cin>>n;
    a[1][n/2+1]=1;

    for(int k=2;k<=n*n;k++)
    {
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    {
    if(a[i][j]==(k-1)){
    if(i==1)
    {
    if(j==n)a[2][j]=k;
    else a[n][j+1]=k;
    }
    if(i!=1)
    {
    if(j==n)a[i-1][1]=k;
    else {
    if(a[i-1][j+1]==0)a[i-1][j+1]=k;
    else a[i+1][j]=k;
    }
    }
    }
    }
    }
    for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
    cout<<a[i][j]<<" ";
    }
    cout<<endl;
    }
    return 0;
    }

  • 1
    @ 2016-10-28 23:02:06

    program magic;
    var
    n,i,j,k,px,py:integer;
    f:array [-1..100,-1..100] of integer;
    begin
    read(n);
    fillchar(f,sizeof(f),0);
    px:=1; py:=(n+1) div 2;
    f[px,py]:=1;
    for k:=2 to n*n do
    begin
    if (px=1) and (py<>n) then
    begin
    px:=n; py:=py+1;
    f[px,py]:=k;
    continue;
    end;
    if (py=n) and (px<>1) then
    begin
    px:=px-1; py:=1;
    f[px,py]:=k;
    continue;
    end;
    if (px=1) and (py=n) then
    begin
    px:=px+1;
    f[px,py]:=k;
    end;
    if (px<>1) and (py<>n) then
    begin
    if (f[px-1,py+1]=0) then
    begin
    px:=px-1; py:=py+1;
    f[px,py]:=k;
    end
    else
    begin
    px:=px+1;
    f[px,py]:=k;
    end;
    end;
    end;
    for i:=1 to n do
    begin
    for j:=1 to n-1 do
    begin
    write(f[i,j],' ');
    end;
    writeln(f[i,n]);
    end;
    end.

  • 1
    @ 2016-08-26 09:44:29

    var
    a:array[0..101,0..101] of longint;
    x,y,i,j,n:longint;
    f1:boolean;
    begin
    readln(n);
    x:=1;y:=(n+1) div 2;
    a[x,y]:=1;
    for i:=2 to n*n do
    begin
    f1:=true;
    if f1 and (x=1) and (y<>n) then begin x:=n;y:=y+1;f1:=false;end;
    if f1 and (y=n) and (x<>1) then begin x:=x-1;y:=1;f1:=false;end;
    if f1 and (x=1) and (y=n) then begin x:=x+1;f1:=false;end;
    if f1 and (x<>1) and (y<>n) then
    begin
    if a[x-1,y+1]=0 then begin x:=x-1;y:=y+1;end else
    x:=x+1;
    end;
    a[x,y]:=i;
    end;
    for i:=1 to n do
    begin
    for j:=1 to n do
    write(a[i,j],' ');
    writeln;
    end;
    end.

  • 0
    @ 2022-07-20 13:49:50
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int n,i,j,a[1010][1010],c,x,k,m;
    int main()
    {
    
    cin>>n;
    a[1][n/2+1]=1;
    k=1;
    x=1;
    c=n/2+1;
    m=n;
    n=n*n-1;
    while(n>0)
    {
    n--;
    k++;
    if(x==1&&c!=m)
    {
    x=m;c++;
    }
    else if(x!=1&&c==m)
    {
    x--;c=1;
    }
    else if(x==1&&c==m)
    x++;
    else if(x!=1&&c!=m&&a[x-1][c+1]==0)
    {
    x--;c++;
    }
    else if(x!=1&&c!=m&&a[x-1][c+1]!=0)
    x++;
    a[x][c]=k;
    }
    for(i=1;i<=m;i++)
    {
    for(j=1;j<=m;j++)
    cout<<a[i][j]<<" ";
    cout<<endl;
    }
    return 0;
    }
    
  • 0
    @ 2018-08-20 09:15:46

    水题 直接模拟

    #include<cstdio> 
    #include<iostream> 
    #include<cstring> 
    #include<algorithm>
    using namespace std;
    const int maxn=40;
    int n,a[maxn][maxn],x,y;
    int main()
    {
    scanf("%d",&n);
    a[1][n/2+1]=1;
    x=1,y=(n/2)+1;
    for(int i=2;i<=n*n;i++)
    {
    if((x==1)&&(y!=n))x=n,y+=1;
    else
    if((x!=1)&&(y==n))y=1,x-=1;
    else
    if((x==1)&&(y==n))x=2,y=n;
    else
    if(a[x-1][y+1]==0)x-=1,y+=1;
    else
    x+=1,y=y;
    a[x][y]=i;
    }
    for(int i=1;i<=n;i++)
    {
    for(int j=1;j<=n;j++)printf("%d ",a[i][j]);
    printf("\n");
    
    }
    return 0;
    }
    
  • 0
    @ 2017-10-29 09:54:59
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int a[1010][1010];
    int main()
    {
        //freopen("square.in","r",stdin);
        //freopen("square.out","w",stdout);
        int n,x=1,y;
        cin>>n;
        y=n/2+1;
        a[x][y]=1;
        for(int i=2;i<=n*n;i++)
        {
            if(x==1&&y!=n)
            {
                x=n;y=y+1;
                a[x][y]=i;
                continue;
            }
            if(x!=1&&y==n)
            {
                x=x-1;y=1;
                a[x][y]=i;
                continue;
            }
            if(x==1&&y==n)
            {
                x++;
                a[x][y]=i;
                continue;
            }
            if(x!=1&&y!=n)
            {
                if(a[x-1][y+1]==0)
                {
                    x--;y++;
                    a[x][y]=i;
                    continue;
                }
                else
                {
                    x++;
                    a[x][y]=i;
                    continue;
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
        return 0;
    }
    
  • 0
    @ 2017-10-20 14:49:11

    简单到爆的白菜法。(・`ω´・)

    #include<iostream>
    #include<cstring>
    using namespace std;
    int main()
    {
        int n;
        int a[40][40];
        memset(a,0,sizeof(a));
        cin>>n;
        a[1][n/2+1]=1;
        for(int k=2;k<=n*n;++k)
        {
            for(int i=1;i<=n;++i)
            {
                for(int j=1;j<=n;++j)
                {
                    if(a[i][j]==k-1)
                    {
                        if(i==1&&j!=n)
                        a[n][j+1]=k;
                        if(i!=1&&j==n)
                        a[i-1][1]=k;
                        if(i==1&&j==n)
                        a[2][n]=k;
                        if(i!=1&&j!=n)
                        {
                            if(a[i-1][j+1]==0)
                            a[i-1][j+1]=k;
                            else
                            a[i+1][j]=k;
                        }
                    }
                    
                }
            }
        }
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=n;++j)
            {
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
        return 0;
    }
    
  • 0
    @ 2017-10-04 00:19:11

    简单到爆的萝卜法。
    #include <iostream>
    using namespace std;
    int arr[41][41];
    int n;
    void givennumber(int a, int b, int c) {
    if (a == 1) {
    arr[b][c] = 1;
    givennumber(2,n,c+1);
    } else {
    if (a <= n * n) {
    arr[b][c] = a;
    int number=0;
    if ((b == 1) && (c != n)) {
    givennumber(a + 1, n, c + 1);
    number=1;
    }
    if ((c == n) && (b != 1)) {
    givennumber(a + 1, b - 1, 1);
    number=1;
    }
    if ((b == 1) && (c == n)) {
    givennumber(a + 1, 2, n);
    number=1;
    }
    if(number==0){
    if (arr[b - 1][c + 1] != 0) {
    givennumber(a + 1, b + 1, c);
    } else {
    givennumber(a + 1, b - 1, c + 1);
    }
    }
    }
    }
    }
    int main() {
    cin >> n;
    if (n == 1) {
    cout << 1;
    } else {
    givennumber(1, 1, (n + 1) / 2);
    for (int i = 1; i <= n; i++) {
    for (int j = 1; j < n; j++) {
    cout << arr[i][j] << ' ';
    }
    cout << arr[i][n] << endl;
    }
    }

    return 0;
    }

  • 0
    @ 2016-05-09 23:30:51

    #include <cstdio>

    int main(){
    int n;
    scanf("%d",&n);
    int g[50][50]={0};
    int x[2500],y[2500];
    g[1][(n+1)/2]=1;
    x[1]=1;
    y[1]=(n+1)/2;
    int px,py;
    for(int k=2;k<=n*n;k++){
    if(x[k-1]==1&&y[k-1]!=n)
    px=n,py=y[k-1]+1;
    if(y[k-1]==n&&x[k-1]!=1)
    py=1,px=x[k-1]-1;
    if(x[k-1]==1&&y[k-1]==n)
    px=x[k-1]+1,py=y[k-1];
    if(x[k-1]!=1&&y[k-1]!=n)
    if(g[x[k-1]-1][y[k-1]+1]==0)
    px=x[k-1]-1,py=y[k-1]+1;
    else
    px=x[k-1]+1,py=y[k-1];
    g[px][py]=k;
    x[k]=px;
    y[k]=py;
    }
    for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++)
    printf("%d ",g[i][j]);
    printf("\n");
    }
    return 0;
    }

  • 0
    @ 2015-12-27 13:38:09

    #include <iostream>
    using namespace std;
    int a[1001][1001],n;
    void dfs(int,int,int);
    int main()
    {cin>>n;
    dfs(1,n/2+1,1);
    for(int i=1;i<=n;i++)
    {for(int j=1;j<=n;j++)
    cout<<a[i][j]<<" ";cout<<endl;}
    }
    void dfs(int x,int y,int step)
    {if(step==n*n+1) return;
    a[x][y]=step;
    int tx=x-1,ty=y+1;
    if(tx<1) tx=tx+n;
    if(tx>n) tx=tx-n;
    if(ty<1) ty=ty+n;
    if(ty>n) ty=ty-n;
    if(a[tx][ty]==0) dfs(tx,ty,step+1);
    else dfs(x+1,y,step+1);
    }

  • -1
    @ 2017-10-22 15:10:18
    int main()
    {
        char *d;
      while (1) d=new char[2];
    }
    
    
  • -1
    @ 2016-11-03 19:04:05

    #include <iostream>
    #include <cstring>

    using namespace std;

    int map[40][40];
    int n;

    int main()
    {
    int i,k;
    cin>>n;
    int mid=(n+1)/2;
    int l=n*n;
    int x[1600],y[1600];
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
    k=1;
    x[k]=1;
    y[k]=mid;
    map[1][mid]=k;
    do
    {
    k++;
    int m=k-1;
    if(x[m]==1&&y[m]!=n)
    {
    map[n][y[m]+1]=k;
    x[k]=n;
    y[k]=y[m]+1;
    }else if(x[m]!=1&&y[m]==n)
    {
    map[x[m]-1][1]=k;
    x[k]=x[m]-1;
    y[k]=1;
    }else if(x[m]==1&&y[m]==n)
    {
    map[x[m]+1][y[m]]=k;
    x[k]=x[m]+1;
    y[k]=y[m];

    }else if(x[m]!=1&&y[m]!=n&&map[x[m]-1][y[m]+1]==0)
    {
    map[x[m]-1][y[m]+1]=k;
    x[k]=x[m]-1;
    y[k]=y[m]+1;
    }else if(x[m]!=1&&y[m]!=n&&map[x[m]-1][y[m]+1]!=0)
    {
    map[x[m]+1][y[m]]=k;
    x[k]=x[m]+1;
    y[k]=y[m];
    }
    }while(k<l);
    for(i=1;i<=n;++i)
    {
    for(k=1;k<=n;++k)
    cout<<map[i][k]<<' ';
    cout<<endl;
    }
    // system("pause");
    return 0;
    }

  • -1
    @ 2016-11-03 18:25:34

    //C语言数学解法

    #include <stdio.h>
    #define MAXn 40

    int main()
    {
    int magic[MAXn][MAXn]={{0}};
    int i,j,k,n;
    scanf("%d",&n);
    i=n;j=n/2+1;k=1;
    magic[i][j]=k;
    while(1){
    k++;i++;j++;
    if(i>n) i-=n;
    if(j>n) j-=n;
    if(magic[i][j]==0){
    magic[i][j]=k;
    continue;
    }
    else {
    i-=2;j--;
    if(i<1) i+=n;
    if(j<1) j+=n;
    if(magic[i][j]==0){
    magic[i][j]=k;
    continue;
    }
    else break;
    }
    }
    for(i=n;i>=1;i--)
    for(j=1;j<=n;j++)
    printf("%d%c",magic[i][j],((j==n)?('\n'):(' ')));
    return 0;
    }

  • -1
    @ 2016-11-01 19:48:37

    program p1978;
    var n,i,j,prei,prej:longint;
    a:array[1..40,1..40]of longint;
    procedure writeout;
    begin
    for i:=1 to n do
    begin
    for j:=1 to n do
    write(a[i,j],' ');
    writeln;
    end;
    end;
    procedure build;
    begin
    a[1,n div 2+1]:=1;prei:=1;prej:=n div 2+1;
    for i:=2 to n*n do
    begin
    if prei=1 then
    begin
    if prej=n then
    begin
    inc(prei);
    a[prei,prej]:=i;
    end
    else
    begin
    prei:=n;
    inc(prej);
    a[prei,prej]:=i;
    end;
    end
    else
    begin
    if prej=n then
    begin
    prej:=1;
    dec(prei);
    a[prei,prej]:=i;
    end
    else
    begin
    if a[prei-1,prej+1]=0 then
    begin
    dec(prei);inc(prej);
    a[prei,prej]:=i;
    end
    else
    begin
    inc(prei);
    a[prei,prej]:=i;
    end;
    end;
    end;
    end;
    end;
    begin
    readln(n);
    fillchar(a,sizeof(a),0);
    build;
    writeout;
    end.

  • -1
    @ 2016-06-04 17:49:11

    pascal?

信息

ID
1978
难度
2
分类
(无)
标签
递交数
2443
已通过
1340
通过率
55%
被复制
9
上传者