N阶奇数幻方(神奇的幻方)#6509#2268

Kinghero King of the summit 2023-08-12 13:40:21 2023-08-12 13:48:29 0
#include <bits/stdc++.h> 
using namespace std;
int main()
{
	int n,a[105][105];
	cin>>n;
	int i = 1;
	int j = n / 2 + 1;
	a[i][j] = 1;
	for(int k = 2;k <= n * n;k++)
	{
		if(k % n == 1)//斜着填应经有数据了 
		{
			i++;//写在当前列的下一行 
		}
		else
		{
			i--;
			j++;
			if(i < 1)//上越界
			{
				i = n;
			}
			if(j > n)//右越界
			{
				j = 1;
			}
		}
		//斜着放数据 
		a[i][j] = k;
	}
	//输出数组
	for(int i = 1;i <= n;i++)
	{
		for(int j = 1;j <= n;j++)
		{
			cout<<a[i][j]<<" ";
		}
		cout<<endl;
	 } 
	return 0;
}
{{ vote && vote.total.up }}

共 2 条回复

Kinghero King of the summit

【狗头】

Even226 逗比

请不要发答案