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

这里可以知道n一定是奇数,所以n*n也一定是奇数,先要每一行,每一列,每一斜行的和都是奇数,就要保证他们上边都有奇数个奇数

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
vector<int> v1,v2; 
int vis[55][55];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n*n;i++){
		if(i%2==1)
		    v1.push_back(i);//奇数 
		else
		    v2.push_back(i);//偶数 
	}
	for(int i=1;i<=n/2;i++){
        for(int j=n/2-i+2;j<(n/2-i+1+2*i);j++){
        	vis[i][j]=1;
		}
	}
	for(int i=n/2+1;i<=n;i++){
		for(int j=(i-n/2);j<=(i-n/2+1+2*n-2*i-1);j++){
			vis[i][j]=1;
		}
	} 
	int n1=0,n2=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(j!=1)
			    cout<<" ";
			if(vis[i][j]==1){
				cout<<v1[n1] ;
				n1++;
			}
			else {
				cout<<v2[n2];
				n2++;
			}
		}
		cout<<endl;
	}
	
}

猜你喜欢

转载自blog.csdn.net/red_red_red/article/details/88423855