Hackerrank – Apple and Orange
Hackerrank – Popis problému Celý popis zadania sa nacháza – Hackerrank. Riešenie Musíme spočítať, koľko jabĺk alebo pomarančov sa nachádza medzi zadanými bodmi na priamke. Použijeme túto rovnicu. Vytvoril som riešenie v týchto programovacích jazykoch: Scala Java JavaScript 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 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.*; public class AppleAndOrange { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int s = stdin.nextInt(); int t = stdin.nextInt(); int a = stdin.nextInt(); int b = stdin.nextInt(); int m = stdin.nextInt(); int n = stdin.nextInt(); int[] apples = new int[m]; for(int i = 0; i < m; i++) { apples[i] = stdin.nextInt(); } int[] oranges = new int[n]; for(int i = 0; i < n; i++) { oranges[i] = stdin.nextInt(); } int fallenApples = (int) Arrays.stream(apples).filter(ap -> (a + ap >= s) && (a + ap <= t)).count(); int fallenOranges = (int) Arrays.stream(oranges).filter(o -> (b + o >= s) && (b + o <= t)).count(); System.out.println(fallenApples); System.out.println(fallenOranges); stdin.close(); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } function main() { var s_temp = readLine().split(' '); var s = parseInt(s_temp[0]); var t = parseInt(s_temp[1]); var a_temp = readLine().split(' '); var a = parseInt(a_temp[0]); var b = parseInt(a_temp[1]); var m_temp = readLine().split(' '); var m = parseInt(m_temp[0]); var n = parseInt(m_temp[1]); apple = readLine().split(' '); apple = apple.map(Number); orange = readLine().split(' '); orange = orange.map(Number); var fallenApples = apple.filter(function(ap) { return (a + ap >= s) && (a + ap <= t); }); var fallenOranges = orange.filter(function(o) { return (b + o >= s) && (b + o <= t); }); console.log(fallenApples.length); console.log(fallenOranges.length); } |
Scala […]