Algorithm
Problem Name: beecrowd | 1827
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1827
Square Array IV
By Neilor Tonin, URI Brazil
Timelimit: 1
Your work in this program is to read an integer number that is the size of a square matrix (width and height) to be filled as follows: the outer part is filled with 0 in the inner part is filled with 1, the main diagonal is filled with 2, the secondary diagonal is filled with 3 and the central element is 4, as the examples below.
Obs: square with '1' always starts at position size / 3, considering width and height and both begin in 0 (zero).
Input
The input contains a number of test cases and ends with EOF (end of file). Each test case consists of an odd integer number N (5 ≤ N ≤ 101) that is the size of the array. For each test case, print the corresponding array as below. After each test case, print a blank line.
Output
For each test case, print the corresponding array as below. After each test case, print a blank line.
Input Sample | Output Sample |
5 |
20003 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<iostream>
using namespace std;
int main() {
int N;
while(cin >> N) {
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
int v = i==N/2 and j==N/2 ? 4 :
i>=N/3 and i < N-N/3 and
j >= N/3 and j < N-N/3 ? 1 :
i == j ? 2 :
i+j+1 == N ? 3 : 0;
cout << v;
}
cout << endl;
}
cout << endl;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto) { return lines.shift();};
while (true) {
var size = parseInt(prompt());
if (isNaN(size)) {
break;
}
for (let i = 0; i < size; i++) {
let line = [];
for (let n = 0; n < size; n++) {
let aux = 0;
if (i == n) {
aux = 2;
}
if (i + n + 1 == size) {
aux = 3;
}
if (
i >= Math.trunc(size / 3) &&
n >= Math.trunc(size / 3) &&
i < size - Math.trunc(size / 3) &&
n < size - Math.trunc(size / 3)
) {
aux = 1;
}
if (
i == Math.trunc(size / 2) &&
n == Math.trunc(size / 2) &&
size % 2 == 1
) {
aux = 4;
}
line.push(aux);
}
console.log(line.join(""));
}
console.log("");
}
Copy The Code &
Try With Live Editor
Input
Output