Algorithm


Problem Name: beecrowd | 1478

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

Square Matrix II

 

By Josué Pereira de Castro, Unioeste Brazil

Timelimit: 1

Write a program that read an integer number N (0 ≤ N ≤ 100) that correspont 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   2
  2   1

  1   2   3
  2   1   2
  3   2   1

  1   2   3   4
  2   1   2   3
  3   2   1   2
  4   3   2   1

  1   2   3   4   5
  2   1   2   3   4
  3   2   1   2   3
  4   3   2   1   2
  5   4   3   2   1

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include<stdio.h>
int main()
{
    int i,j,k,m,n;
    for(;;)
    {
        scanf("%d",&n);
        if(n==0)break;
        int ar[n][n];
        for(i = 0; i < n; i++)
        {
            m = i+1;
            k = 2;
            for(j = 0; j  < = i; j++, m--)
            {
                ar[i][j] = m;
            }
            for(j= i+1; j  <  n; j++, k++)
                ar[i][j] = k;

        }

        for(i = 0; i  <  n; i++)
        {
            for(j = 0; j <  n; j++)
            {
                if(j==0)printf("%3d",ar[i][j]);
                else
                    printf(" %3d",ar[i][j]);

            }
            printf("\n");
        }
        printf("\n">;
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
1 2 3 4 5 0

Output

x
+
cmd
1 1 2 2 1 1 2 3 2 1 2 3 2 1 1 2 3 4 2 1 2 3 3 2 1 2 4 3 2 1 1 2 3 4 5 2 1 2 3 4 3 2 1 2 3 4 3 2 1 2 5 4 3 2 1

#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();};

var size = parseInt(prompt());

while (size > 0) {
  for (let i = 0; i  <  size; i++) {
    let line = [];

    for (let n = 0; n  <  size; n++) {
      let aux = (1 + Math.abs(i - n)).toFixed(0);
      line.push(aux.padStart(3));
    }

    console.log(line.join(" "));
  }

  console.log("");
  size = parseInt(prompt());
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
1 2 3 4 5 0

Output

x
+
cmd
1 1 2 2 1 1 2 3 2 1 2 3 2 1 1 2 3 4 2 1 2 3 3 2 1 2 4 3 2 1 1 2 3 4 5 2 1 2 3 4 3 2 1 2 3 4 3 2 1 2 5 4 3 2 1
Advertisements

Demonstration


Previous
#1472 Beecrowd Online Judge Solution 1472 Triangles Solution in C, C++, Java, Js and Python
Next
#1483 Beecrowd Online Judge Solution 1483 Animal Game Solution in C, C++, Java, Js and Python