710C - Magic Odd Square - CodeForces Solution


constructive algorithms math *1500

Please click on ads to support us..

C++ Code:

/* {وَقُلْ رَبِّ زِدْنِي عِلْمًاً} */
#include<bits/stdc++.h>
using namespace std;
#define TY cin.tie(0), cout.tie(0),cin.sync_with_stdio(0), cout.sync_with_stdio(0);
const int N = 1000;
int arr[N][N];
int main() {
    TY
    int n;
    cin >> n;
    int ev = 2, odd = 1;
    for (int i = 0; i < n / 2; ++i) {
        int k = n / 2 - i;
        for (int j = 0; j < k; ++j) {
            arr[i][j] = ev, ev += 2;
            arr[n - i - 1][j] = ev, ev += 2;
            arr[i][n - j - 1] = ev, ev += 2;
            arr[n - i - 1][n - j - 1] = ev, ev += 2;
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j)
            if (arr[i][j])cout << arr[i][j] << " ";
            else cout << odd << " ", odd += 2;
        cout << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why