Algorithm


Problem Name: beecrowd | 2750

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

Output 4

 

By Roberto A. Costa Jr, UNIFEI BR Brazil

Timelimit: 1

Your teacher would like you to do a program with the following characteristics:

  1. Create 16 integer variables;
  2. Assign them values ​​from 0 to 15 to each of the previous variables;
  3. Have 39 dashes (-) on the first line;
  4. Have a | under the first dash, thirteenth, twenty-third and the thirty-ninth dash of the first line, below the 4th dash should begin to write "decimal", under the 16th dash should begin to write "octal", under the 26th dash should begin write "Hexadecimal" and the rest fill in with whitespace;
  5. Repeat procedure 1;
  6. Have a | under the first dash, the thirteenth, the twenty-third and the thirty-ninth dash of the first line, below the 8th dash should print the value of the first variable in decimal value, below the 18th dash should print the value of the first variable in octal value, underneath of the 31st dash must print the value of the first variable in hexadecimal value and the rest fill in with whitespace;
  7. Repeat procedure 6 for the other 15 variables;
  8. Repeat procedure 1.

At the end should look like the following image:

--------------------------------------- (39 traces)
| decimal   |  octal  |  Hexadecimal  |
---------------------------------------
|      0    |    0    |       0       |
|      1    |    1    |       1       |
|      2    |    2    |       2       |
|      3    |    3    |       3       |
|      4    |    4    |       4       |
|      5    |    5    |       5       |
|      6    |    6    |       6       |
|      7    |    7    |       7       |
|      8    |   10    |       8       |
|      9    |   11    |       9       |
|     10    |   12    |       A       |
|     11    |   13    |       B       |
|     12    |   14    |       C       |
|     13    |   15    |       D       |
|     14    |   16    |       E       |
|     15    |   17    |       F       |
--------------------------------------- (39 traces)

 

Input

 

There is not.

 

Output

 

The output will print as shown above.

 

 

 

Input Sample Output Sample
 
---------------------------------------
| decimal   |  octal  |  Hexadecimal  |
---------------------------------------
|      0    |    0    |       0       |
|      1    |    1    |       1       |
|      2    |    2    |       2       |
|      3    |    3    |       3       |
|      4    |    4    |       4       |
|      5    |    5    |       5       |
|      6    |    6    |       6       |
|      7    |    7    |       7       |
|      8    |   10    |       8       |
|      9    |   11    |       9       |
|     10    |   12    |       A       |
|     11    |   13    |       B       |
|     12    |   14    |       C       |
|     13    |   15    |       D       |
|     14    |   16    |       E       |
|     15    |   17    |       F       |
---------------------------------------

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

int main() { 
    printf("---------------------------------------\n");
    printf("|  decimal  |  octal  |  Hexadecimal  |\n");
    printf("---------------------------------------\n");
    printf("|      0    |    0    |       0       |\n");
    printf("|      1    |    1    |       1       |\n");
    printf("|      2    |    2    |       2       |\n");
    printf("|      3    |    3    |       3       |\n");
    printf("|      4    |    4    |       4       |\n");
    printf("|      5    |    5    |       5       |\n");
    printf("|      6    |    6    |       6       |\n");
    printf("|      7    |    7    |       7       |\n");
    printf("|      8    |   10    |       8       |\n");
    printf("|      9    |   11    |       9       |\n");
    printf("|     10    |   12    |       A       |\n");
    printf("|     11    |   13    |       B       |\n");
    printf("|     12    |   14    |       C       |\n");
    printf("|     13    |   15    |       D       |\n");
    printf("|     14    |   16    |       E       |\n");
    printf("|     15    |   17    |       F       |\n");
    printf("---------------------------------------\n");
}

Copy The Code & Try With Live Editor

Output

x
+
cmd
--------------------------------------- | decimal | octal | Hexadecimal | --------------------------------------- | 0 | 0 | 0 | | 1 | 1 | 1 | | 2 | 2 | 2 | | 3 | 3 | 3 | | 4 | 4 | 4 | | 5 | 5 | 5 | | 6 | 6 | 6 | | 7 | 7 | 7 | | 8 | 10 | 8 | | 9 | 11 | 9 | | 10 | 12 | A | | 11 | 13 | B | | 12 | 14 | C | | 13 | 15 | D | | 14 | 16 | E | | 15 | 17 | F | ---------------------------------------

#2 Code Example with Java Programming

Code - Java Programming


public class Main {
	public static void main(String[] args) {
		int[] decimal = new int[16];
		String[] octal = new String[16];
		String[] hexa = new String[16];
		int i;
		for(i=0 ; i < 16 ; i++){
			decimal[i]=i;
			octal[i]=Integer.toString(decimal[i],8);
			hexa[i]=Integer.toString(decimal[i],16).toUpperCase();
		}
		System.out.printf("---------------------------------------\n");
		System.out.printf("| decimal  | octal  | Hexadecimal  |\n");
		System.out.printf("---------------------------------------\n");
		for(i=0 ; i < 16 ; i++)
			System.out.printf("| %2d    | %2s    | %2s       |\n",decimal[i],octal[i],hexa[i]);
		System.out.printf("---------------------------------------\n");
	}
}

Copy The Code & Try With Live Editor

Output

x
+
cmd
--------------------------------------- | decimal | octal | Hexadecimal | --------------------------------------- | 0 | 0 | 0 | | 1 | 1 | 1 | | 2 | 2 | 2 | | 3 | 3 | 3 | | 4 | 4 | 4 | | 5 | 5 | 5 | | 6 | 6 | 6 | | 7 | 7 | 7 | | 8 | 10 | 8 | | 9 | 11 | 9 | | 10 | 12 | A | | 11 | 13 | B | | 12 | 14 | C | | 13 | 15 | D | | 14 | 16 | E | | 15 | 17 | F | ---------------------------------------

#3 Code Example with Javascript Programming

Code - Javascript Programming


const isSuccessfullyDefined = Reflect.defineProperty(String.prototype, "pad", {
	value(pad, from = 0) {
		const splited = [...this]
		splited.splice(from, pad.length, ...pad)
		return splited.join("").substr(0, this.length)
	},
	writable: false,
	configurable: false,
	enumerable: true,
})

const UNTIL = 15
const ROW_SIZE = 39

const SPACE = " "
const DIV = "|"
const DASH = "-"

function main() {
	const separator = DASH.repeat(ROW_SIZE) // 	"---------------------------------------"

	const row = SPACE.repeat(ROW_SIZE) 			//	"|           |         |               |"
		.pad(DIV, 0)
		.pad(DIV, 12)
		.pad(DIV, 22)
		.pad(DIV, -1)

	const label = row 				//		"|  decimal  |  octal  |  Hexadecimal  |"
		.pad("decimal", 3)
		.pad("octal", 15)
		.pad("Hexadecimal", 25)

	const output = [
		separator,
		label,
		separator
	]

	for (let num = 0; num  < = UNTIL; num++) {
		const dec = num.toString(10).toUpperCase()
		const oct = num.toString(8).toUpperCase()
		const hex = num.toString(16).toUpperCase()

		const currentRow = row
			.pad(dec, 8 - dec.length)
			.pad(oct, 18 - oct.length)
			.pad(hex, 31 - hex.length)

		output.push(currentRow)
	}

	console.log(output.concat(separator).join("\n"))
}

if (isSuccessfullyDefined) {
	main()
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
--------------------------------------- | decimal | octal | Hexadecimal | --------------------------------------- | 0 | 0 | 0 | | 1 | 1 | 1 | | 2 | 2 | 2 | | 3 | 3 | 3 | | 4 | 4 | 4 | | 5 | 5 | 5 | | 6 | 6 | 6 | | 7 | 7 | 7 | | 8 | 10 | 8 | | 9 | 11 | 9 | | 10 | 12 | A | | 11 | 13 | B | | 12 | 14 | C | | 13 | 15 | D | | 14 | 16 | E | | 15 | 17 | F | ---------------------------------------

#4 Code Example with Python Programming

Code - Python Programming


print('---------------------------------------')
print('|  decimal  |  octal  |  Hexadecimal  |')
print('---------------------------------------')
print('|      0    |    0    |       0       |')
print('|      1    |    1    |       1       |')
print('|      2    |    2    |       2       |')
print('|      3    |    3    |       3       |')
print('|      4    |    4    |       4       |')
print('|      5    |    5    |       5       |')
print('|      6    |    6    |       6       |')
print('|      7    |    7    |       7       |')
print('|      8    |   10    |       8       |')
print('|      9    |   11    |       9       |')
print('|     10    |   12    |       A       |')
print('|     11    |   13    |       B       |')
print('|     12    |   14    |       C       |')
print('|     13    |   15    |       D       |')
print('|     14    |   16    |       E       |')
print('|     15    |   17    |       F       |')
print('---------------------------------------')

Copy The Code & Try With Live Editor

Output

x
+
cmd
--------------------------------------- | decimal | octal | Hexadecimal | --------------------------------------- | 0 | 0 | 0 | | 1 | 1 | 1 | | 2 | 2 | 2 | | 3 | 3 | 3 | | 4 | 4 | 4 | | 5 | 5 | 5 | | 6 | 6 | 6 | | 7 | 7 | 7 | | 8 | 10 | 8 | | 9 | 11 | 9 | | 10 | 12 | A | | 11 | 13 | B | | 12 | 14 | C | | 13 | 15 | D | | 14 | 16 | E | | 15 | 17 | F | ---------------------------------------
Advertisements

Demonstration


Previous
#2749 Beecrowd Online Judge Solution 2749 Output 3 Solution in C, C++, Java, Js and Python
Next
#2752 Beecrowd Online Judge Solution 2752 Output 6 Solution in C, C++, Java, Js and Python