Hackerrank – Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Načítaj všetky hodnotenia a porovnaj ich nasledujúcimi podmienkami:
- Ak
ai > bi
, potom dostane Alice 1 bod - Ak
ai < bi
, Bob dostane bod - Ak
ai = bi
, nikto nedostane bod
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 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.*; public class CompareTheTriplets { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a0 = in.nextInt(); int a1 = in.nextInt(); int a2 = in.nextInt(); int b0 = in.nextInt(); int b1 = in.nextInt(); int b2 = in.nextInt(); int aliceScore = 0; int bobScore = 0; if(a0 > b0) { aliceScore++; } if(a1 > b1) { aliceScore++; } if(a2 > b2) { aliceScore++; } if(a0 < b0) { bobScore++; } if(a1 < b1) { bobScore++; } if(a2 < b2) { bobScore++; } System.out.println(aliceScore + " " + bobScore); in.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 |
'use strict'; const processData = input => { const lines = input.split('\n'); const alicesRating = lines[0].split(' ').map(i => parseInt(i)); const bobRating = lines[1].split(' ').map(i => parseInt(i)); let aliceScore = 0; let bobScore = 0; for(let i = 0; i < alicesRating.length; i++) { if (alicesRating[i] > bobRating[i]) { aliceScore++; } else if (alicesRating[i] < bobRating[i]) { bobScore++; } } console.log(aliceScore + ' ' + bobScore); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 |
object CompareTheTriplets extends App { val lines = Source.stdin.getLines().toList val alicesRating = lines(0).split(" ").map(_.toInt) val bobsRating = lines(1).split(" ").map(_.toInt) val differentRatings = alicesRating.zip(bobsRating).filter(pair => pair._1 != pair._2) val aliceScore = differentRatings.count(pair => pair._1 > pair._2) val bobScore = differentRatings.length - aliceScore println(aliceScore + " " + bobScore) } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
a0, a1, a2 = gets.strip.split(' ') a0 = a0.to_i a1 = a1.to_i a2 = a2.to_i b0, b1, b2 = gets.strip.split(' ') b0 = b0.to_i b1 = b1.to_i b2 = b2.to_i alice_score = 0 bob_score = 0 alice_score = alice_score + 1 if a0 > b0 alice_score = alice_score + 1 if a1 > b1 alice_score = alice_score + 1 if a2 > b2 bob_score = bob_score + 1 if a0 < b0 bob_score = bob_score + 1 if a1 < b1 bob_score = bob_score + 1 if a2 < b2 puts "#{alice_score} #{bob_score}" |