Popis problému Celý popis zadania sa nacháza – Hackerrank. Riešenie Vytvoril som riešenie v týchto programovacích jazykoch: Java JavaScript Scala Ruby Všetky riešenia sú dostupné aj na mojom GitHub profile. Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
import java.util.*; public class ArraysDS { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int length = stdin.nextInt(); int[] array = new int[length]; for(int i = 0; i < length; i++) { array[i] = stdin.nextInt(); } for(int i = length - 1; i >= 0; i--) { System.out.print(array[i] + " "); } stdin.close(); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
'use strict'; const processData = input => { let lines = input.split('\n'); let l = parseInt(lines[0]); let array = lines[1].split(' ').map(item => parseInt(item)); let reversed = []; for(let i = l - 1; i >= 0; i--) { reversed.push(array[i]); } console.log(reversed.join(" ")); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
|
object ArraysDS { def main(args: Array[String]) { val sc = new java.util.Scanner (System.in) var n = sc.nextInt() var arr = new Array[Int](n) for(arr_i <- 0 to n-1) { arr(arr_i) = sc.nextInt() } println(arr.reverse.mkString(" ")) } } |
Ruby
|
n = gets.strip.to_i arr = gets.strip arr = arr.split(' ').map(&:to_i) puts arr.reverse.join(" ") |