CodeForces710C Magic Old 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.

Example

Input
1
Output
1
Input
3
Output
2 1 4
3 5 7
6 9 8

 

真是神奇的构造。http://blog.csdn.net/zsd201531107026/article/details/52348589

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
#include <cctype>
#include <string>

using namespace std;

int arr[50][50], n;

int main() {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i = 1, tot = 1, add = 2, T = 1 ; i <= n ; i ++) {
        if(i == n / 2 + 1) {
            add = -2;
        }
        arr[i][n / 2 + 1] = T; T += 2;
        for(int j = 1 ; j <= tot / 2 ; j ++) {
            arr[i][n / 2 + 1 + j] = T; T += 2;
            arr[i][n / 2 + 1 - j] = T; T += 2;
        }
        tot += add;
    }
    for(int i = 1, T = 2 ; i <= n ; i ++) {
        for(int j = 1 ; j <= n ; j ++) {
            if(arr[i][j] == 0) {
                arr[i][j] = T; T += 2;
            }
        }
    }
    for(int i = 1 ; i <= n ; i ++) {
        for(int j = 1 ; j <= n ; j ++) {
            cout << arr[i][j] << ' ';
        }
        cout << endl;
    }
}

  

posted @ 2017-09-08 21:02  KingSann  阅读(143)  评论(0编辑  收藏  举报