Magic Odd Square (思维+构造)

Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.

Input

The only line contains odd integer n (1 ≤ n ≤ 49).

Output

Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.

Examples

Input

1

Output

1

Input

3

Output

2 1 4
3 5 7
6 9 8

这个题在中间的菱形区域是奇数,其余为偶数

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>

const int maxn=3e5+5;
typedef long long ll;
using namespace std;

int a[maxn];
int b[maxn];
int Map[200][200];
int main()
{
	int n;
	cin>>n;
	memset(Map,0,sizeof(Map));
	int cnt1=0;
	int cnt2=0;
	for(int t=1;t<=n*n;t++)
	{
		if(t%2==0)
		{
			a[cnt1++]=t;
		}
		else
		{
			b[cnt2++]=t;
	    }
	}
	int cnt3=0;
	for(int t=0;t<n;t++)
	{
		for(int j=0;j<n;j++)
		{
			if(j>=n/2-t&&j<=t+n/2&&j<=n-1+n/2-t&&j>=t-n/2)
			{
				Map[t][j]=b[cnt3];
				cnt3++;
			}
		}
	}
	int cnt4=0;
	for(int t=0;t<n;t++)
	{
		for(int j=0;j<n;j++)
		{
			if(Map[t][j]==0)
			{
				Map[t][j]=a[cnt4];
				cnt4++;
			}
		}
	}
	for(int t=0;t<n;t++)
	{
		for(int j=0;j<n;j++)
		{
			cout<<Map[t][j]<<" ";
		}
		cout<<endl;
	}
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

black-hole6

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值