花花绿绿,就10分(悬赏关注)
查看原帖
花花绿绿,就10分(悬赏关注)
1351767
zyx_dzpd楼主2024/7/24 18:13
#include<bits/stdc++.h>
//#define    //不加";"
using namespace std;

int a[100][100],n,i,j,x1=1,x2;

int main()
{
	cin>>n;
	a[1][(n/2+1)]=1;
	x2=(n/2+1);
	for(i=2;i<=n*n;i++){
		if(x1==1&&x2!=n){
			a[n][x2+1]=i;
			x1=n;
			x2=x2+1;
		}
		else if(x1!=1&&x2==n){
			a[x1+1][1]=i;
			x1++;
			x2=n;
		}
		else if(x1==1&&x2==n){
			a[x1+1][x2]=i;
			x1++;
		}
		else if(x1!=1&&x2!=n){
			if(a[x1-1][x2+1]==0) a[x1-1][x2+1]=i,x2+1,x1--;
			else a[x1+1][x2]=i,x1++;
			continue;
		}
	}
	for(i=1;i<=n;i++){
		for(j=1;j<=n;j++)cout<<a[i][j]<<" ";
		cout<<"\n";
	}
    return 0;
}

2024/7/24 18:13
加载中...