Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Sum all elements. The result is sum / 2
.
I created solution in:
All solutions are also available on my GitHub.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.*; public class BdayGift { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = stdin.nextInt(); long sum = 0; for(int i = 0; i < tests; i++) { sum += stdin.nextInt(); } System.out.format("%.1f", (double) sum / 2); stdin.close(); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
'use strict'; var _ = require('lodash'); const processData = input => { let arr = input.split('\n').slice(1).map(i => parseInt(i)) let sum = _.sum(arr); console.log((sum / 2) + (sum % 2 == 0 ? '.0' : '')); }; 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 |
import scala.io.Source object BdayGift extends App { val lines = Source.stdin.getLines().drop(1).map(_.toLong) printf("%.1f", lines.sum.toDouble / 2) } |
Ruby
1 2 3 4 5 6 7 8 |
balls = gets.chomp.to_i expected_num = 0 balls.times do input = gets.chomp.to_i expected_num += input end expected_num /= 2.0 puts "#{format('%.1f', expected_num)}" |