求过qwq按照题目模拟的,但是发现xy会循环……
查看原帖
求过qwq按照题目模拟的,但是发现xy会循环……
809923
Jne1206273楼主2022/10/15 10:05
#include<iostream>
using namespace std;
int main(){
	int n;
	cin >> n;
	int a[41][41] = {0};
	int x = 1 , y = (n + 1) / 2;//x,y坐标;存储时-1 
	for(int i = 1; i <= n * n; i++){
		a[x-1][y-1] = i;
		if(x == 1 && y != n){
			x = n;
			y ++;
		}else if(x != 1 && y == n){
			x --;
			y = 1;
		}else if(x == 1 && y == n){
			x++;
		}else if(x != 1 && y != n){
			if(a[x][y] == 0){
				x--;
				y++;
			}else{
				x++;
			}
		}
	} 
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			cout << a[i][j] << ' ';
		}
		cout << endl;
	}
	return 0;
} 
2022/10/15 10:05
加载中...