Algorithm


Problem Name: beecrowd | 1557

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

Square Matrix III

By Gabriel Dalalio, ITA BR Brazil

Timelimit: 1

Write a program that read an integer number N (0 ≤ N ≤ 15) 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 array must be formatted in a field of size T right justified and separated by a space, where T is equal to the number of digits of the biggest number in the array. 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 4

 1  2  4
 2  4  8
 4  8 16

 1  2  4  8
 2  4  8 16
 4  8 16 32
 8 16 32 64

  1   2   4   8  16
  2   4   8  16  32
  4   8  16  32  64
  8  16  32  64 128
 16  32  64 128 256

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include<stdio.h>
#include<string.h>
int main()
{
    long long int i,j,k,n,l,len,m;
    while(scanf("%lld",&n))
    {
        l=1;
        if(n==0)break;
        long long int arr[n][n];
        for(i = 0; i < n; i++)
        {
            k=l;
            for(j = 0; j  <  n; j++)
            {
                arr[i][j]=k;
                k += k;
            }
            l+=l;
        }
        for(i = 0; i  <  n; i++)
        {
            for(j = 0; j  <  n; j++)
            {
                if(n==3 || n==4)
                {
                    if(j==0) printf("%2lld",arr[i][j]);
                    else printf(" %2lld",arr[i][j]);
                }
                else if(n==5)
                {
                    if(j==0) printf("%3lld",arr[i][j]);
                    else printf(" %3lld",arr[i][j]);
                }
                else if(n==6 || n==7)
                {
                    if(j==0) printf("%4lld",arr[i][j]);
                    else printf(" %4lld",arr[i][j]);
                }
                else if(n==8 || n==9)
                {
                    if(j==0) printf("%5lld",arr[i][j]);
                    else printf(" %5lld",arr[i][j]);
                }
                else if(n==10)
                {
                    if(j==0) printf("%6lld",arr[i][j]);
                    else printf(" %6lld",arr[i][j]);
                }
                else if(n==11 || n==12)
                {
                    if(j==0) printf("%7lld",arr[i][j]);
                    else printf(" %7lld",arr[i][j]);
                }
                else if(n==13 || n==14)
                {
                    if(j==0) printf("%8lld",arr[i][j]);
                    else printf(" %8lld",arr[i][j]);
                }
                else if(n==15)
                {
                    if(j==0) printf("%9lld",arr[i][j]);
                    else printf(" %9lld",arr[i][j]);
                }
                else if(n==1)printf("%1lld",arr[i][j]);
                else if(n==2)
                {
                    if(j==0)printf("%lld",arr[i][j]);
                    else printf(" %lld",arr[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 4 1 2 4 2 4 8 4 8 16 1 2 4 8 2 4 8 16 4 8 16 32 8 16 32 64 1 2 4 8 16 2 4 8 16 32 4 8 16 32 64 8 16 32 64 128 16 32 64 128 256

#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) {
  var T = Math.pow(4, size - 1);
  T = T.toFixed(0).length;

  for (let L = 0; L  <  size; L++) {
    let line = [];

    for (let C = 0; C  <  size; C++) {
      let aux = Math.pow(2, L + C).toFixed(0);

      line.push(aux.padStart(T));
    }

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

  console.log("");
  var 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 4 1 2 4 2 4 8 4 8 16 1 2 4 8 2 4 8 16 4 8 16 32 8 16 32 64 1 2 4 8 16 2 4 8 16 32 4 8 16 32 64 8 16 32 64 128 16 32 64 128 256
Advertisements

Demonstration


Previous
#1553 Beecrowd Online Judge Solution 1553 Frequent Asked Questions Solution in C, C++, Java, Js and Python
Next
#1558 Beecrowd Online Judge Solution 1558 Sum of Two Squares Solution in C, C++, Java, Js and Python