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:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import scala.io.Source import java.util.Scanner object AppleAndOrange extends App { val stdin = new Scanner(System.in) val s = stdin.nextInt() val t = stdin.nextInt() val a = stdin.nextInt() val b = stdin.nextInt() val m = stdin.nextInt() val n = stdin.nextInt() val apples = (0 until m).map(_ => stdin.nextInt()) val oranges = (0 until n).map(_ => stdin.nextInt()) val fallenApples = apples.count(ap => (a + ap >= s) && (a + ap <= t)) val fallenOranges = oranges.count(o => (b + o >= s) && (b + o <= t)) println(fallenApples) println(fallenOranges) } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
s,t = gets.strip.split(' ') s = s.to_i t = t.to_i a,b = gets.strip.split(' ') a = a.to_i b = b.to_i m,n = gets.strip.split(' ') m = m.to_i n = n.to_i apple = gets.strip apple = apple.split(' ').map(&:to_i) orange = gets.strip orange = orange.split(' ').map(&:to_i) fallen_apples = apple.select {|ap| (a + ap >= s) && (a + ap <= t)}.count fallen_oranges = orange.select {|o| (b + o >= s) && (b + o <= t)}.count puts fallen_apples puts fallen_oranges |