对,但又错
查看原帖
对,但又错
1276415
abababwosb楼主2024/10/7 11:37

这个程序输出的幻方都是正确的,但是就是和样例不同。

#include <bits/stdc++.h>
using namespace std;

int a[50][50];

int main() {
	int n, x, y, cnt = 2;
	cin >> n;
	x = 1;
	y = n / 2 + 1;
	a[x][y] = 1;
	for (int i = 1; i < n * n; ++i) {
		if (x == 1) {
			x = n;
		} else {
			x--;
		}
		if (y == n) {
			y = 1;
		} else {
			y++;
		}
		if (a[x][y] == 0) {
			a[x][y] = cnt;
			cnt++;
		} else {
			if (x == n) {
				x = 1;
			} else {
				x++;
			}
			a[x][y] = cnt;
			cnt++;
		}
	}
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= n; ++j) {
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}
2024/10/7 11:37
加载中...