求助!!!
查看原帖
求助!!!
1368233
liyifanglan楼主2024/8/21 13:56
#include<bits/stdc++.h>
using namespace std;

int n;
int ii, jj;
int a[25][25];
int main(){
	cin >> n;
	ii = 1, jj = (n+1)/2;
	a[ii][jj] = 1;
	for (int i = 2; i <= n*n; i++){
		int x = ii+1, y = jj;
		if (ii == 1){
			x = n;
		}
		if (jj == n){
			y = 1;
		}
		if (a[x][y] != 0){
			a[x][y] = i;
			ii = x, jj = y;
		}else{
			if (ii == n){
				x = 1;
			}else{
				x--;
			}
			a[x][y] = i;
			ii = x, jj = y;
		}
	}
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= n; j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}
2024/8/21 13:56
加载中...