Hackerrank – Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Spočítaj všetkých študentov, ktorí prišli pred začatím prednášky (ich čas príchodu je menší alebo rovný 0). Pre výsledok stačí skontrolovať, či je počet študentov menší ako hranica pre zrušenie prednášky.
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 |
import java.util.Scanner; public class AngryProfessor { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = Integer.parseInt(stdin.nextLine()); for (int i = 0; i < tests; i++) { String[] inputs = stdin.nextLine().split(" "); int students = Integer.parseInt(inputs[0]); int classCancelation = Integer.parseInt(inputs[1]); String[] arrivals = stdin.nextLine().split(" "); if(classCancelation > studentsBeforeClass(arrivals)) { System.out.println("YES"); } else { System.out.println("NO"); } } stdin.close(); } private static int studentsBeforeClass(String[] arrivals) { int result = 0; for(int i = 0; i < arrivals.length; i++) { if(Integer.parseInt(arrivals[i]) <= 0) { result++; } } return result; } } |
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 |
'use strict'; const processData = input => { const lines = input.split('\n'); const tests = parseInt(lines[0]); let actualLine = 1; for(let i = 0; i < tests; i++) { const studentsAndCancelation = lines[actualLine++].split(' ').map(i => parseInt(i)); const cancelation = studentsAndCancelation[1]; const arrivals = lines[actualLine++].split(' ').map(i => parseInt(i)); const beforeClass = studentsBeforeClass(arrivals); const canceled = cancelation > beforeClass ? "YES" : "NO"; console.log(canceled); } }; const studentsBeforeClass = (arrivals) => { let result = 0; for(let i = 0; i < arrivals.length; i++) { if(arrivals[i] <= 0) { result++; } } return result; }; process.stdin.resume(); process.stdin.setEncoding("ascii"); var _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 12 13 14 15 16 17 18 19 20 |
import scala.io.Source object AngryProfessor extends App { val lines = Source.stdin.getLines().drop(1) val inputs = lines.grouped(2).map(_.toList) inputs.foreach { input => { val studentsAndCancelation = input(0).split("\\s+").map(_.toInt) val students = studentsAndCancelation(0) val cancelation = studentsAndCancelation(1) val arrivals = input(1).split("\\s+").map(_.toInt) val cancelled = if(cancelation > studentsBeforeClass(arrivals)) "YES" else "NO" println(cancelled) } } def studentsBeforeClass(arrivals: Array[Int]): Int = { arrivals.count(_ <= 0) } } |
Ruby
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def before_class(arrivals) result = 0 arrivals.each do |item| result += 1 if item <= 0 end return result end tests = gets.to_i tests.times do |i| (students, cancelation) = gets.split(" ").map {|i| i.to_i} arrivals = gets.split(" ").map {|i| i.to_i} before = before_class(arrivals) if(cancelation > before) puts "YES" else puts "NO" end end |