Algorithm


Problem Name: beecrowd | 1435

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1435

Square Matrix I

 

Adapted by Josué P. de Castro Brazil

Timelimit: 2

Write a program that read an integer number N (0 ≤ N ≤ 100) that correspond to the order of a Bidimentional array of integers, and build the Array according to the above example.

 

Input

 

The input consists of several integers numbers, one per line, corresponding to orders from arrays to be built. The end of input is indicated by zero (0).

 

Output

 

For each integer number of input, print the corresponding array according to the example. (the values ​​of the arrays must be formatted in a field of size 3 right justified and separated by a space. None space must be printed after the last character of each row of the array. A blank line must be printed after each array.

 

 

 

Sample Input Sample Output

1
2
3
4
5
0

  1
 
  1   1
  1   1
 
  1   1   1
  1   2   1
  1   1   1
   
  1   1   1   1
  1   2   2   1
  1   2   2   1
  1   1   1   1
 
  1   1   1   1   1
  1   2   2   2   1
  1   2   3   2   1
  1   2   2   2   1
  1   1   1   1   1

 

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming


var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
let contador = parseInt(lines.shift());
let vetor = [];
let matriz = [];
while (contador !== 0) {
  matriz = new Array(contador);
  for (let i = 0; i  <  contador / 2; i++) {
    vetor = new Array(contador);
    for (let j = 0; j  <  contador / 2; j++) {
      const value = Math.min(i, j) + 1;
      vetor[j] = value  <  10 ? ` ${value}` : `${value}`; 
      vetor[contador - 1 - j] = vetor[j];
    }
    matriz[i] = ` ${vetor.join('  ')}`;
    matriz[contador - 1 - i] = matriz[i];
  }
  console.log(`${matriz.join('\n')}`);

  contador = parseInt(lines.shift());
  console.log('');
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1 2 3 4 5 0

Output

x
+
cmd
1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 2 2 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 2 3 2 1 1 2 2 2 1 1 1 1 1 1

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>
using namespace std;

int main(){
    int n, i, j, mat[100][100] = {}, temp = 1;
    bool is = false;
    while(cin >> n and n != 0){
    for (i = 0;i  <  n;i++) {
        for (j = 0;j  <  n;j++) {
            mat[i][j] = 1;
            if (i > 0 and j > 0 and i  <  n-1 and j < n-1) {
               mat[i][j] += mat[temp][temp];
            } 
            if (j == i) {
                is = true;
            }
        }
        if(is == true){
            temp+=1;
            is = false;
        }
    }
    for (i = 0;i < n;i++) {
        for (j = 0;j  <  n;j++> {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
    }

    return 0;
}
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
#1428 Beecrowd Online Judge Solution 1428 Searching for Nessy Solution in C, C++, Java, Js and Python
Next
#1437 Beecrowd Online Judge Solution 1437 Turn Left! Solution in C, C++, Java, Js and Python