Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
$$handshakes = \sum_{i=0}^{directors-1}{i}$$
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 |
import java.util.*; public class Handshake { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = stdin.nextInt(); for(int i = 0; i < tests; i++) { long handshakes = 0; int directors = stdin.nextInt(); for(int j = 0; j < directors; j++) { handshakes += j; } System.out.println(handshakes); } 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 directors = input.split('\n').map(i => parseInt(i)) for(let i = 1; i < directors.length; i++) { let handshakes = 0; for(let j = 0; j < directors[i]; j++) { handshakes += j; } console.log(handshakes); } }; process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Ruby
1 2 3 4 5 6 7 8 |
gets.chomp.to_i.times do directors = gets.chomp.to_i result = 0 (directors - 1).downto 0 do |handshakes| result += handshakes end puts result end |
Scala
1 2 3 4 5 6 7 8 |
import scala.io.Source object Handshake extends App { val lines = Source.stdin.getLines() val directors = lines.drop(1).map(_.toInt) val handshakes = directors.map(Range(0, _).toList.sum) println(handshakes.mkString("\n")) } |