Hackerrank – Popis problému
Celý popis zadania sa nacháza – Hackerrank.
Riešenie
Použi vzorec zo zadania úlohy.
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 |
import java.util.*; public class ViralAdvertising { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n = Integer.parseInt(stdin.nextLine()); int totalLiked = 0; int dayStartPeople = 5; for(int i = 0; i < n; i++) { int dayLiked = dayStartPeople / 2; int received = dayLiked * 3; totalLiked += dayLiked; dayStartPeople = received; } System.out.println(totalLiked); 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 |
'use strict'; const processData = input => { const n = parseInt(input) let totalLiked = 0; let dayStartPeople = 5; for(let i = 0; i < n; i++) { const dayLiked = parseInt(dayStartPeople / 2); const received = dayLiked * 3; totalLiked += dayLiked; dayStartPeople = received; } console.log(totalLiked); }; 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 12 13 14 |
import scala.io.Source object ViralAdvertising extends App { val n = Source.stdin.bufferedReader().readLine().toInt println(like(5).take(n).sum) def like(start: Long): Stream[Long] = { val liked = start / 2 val received = liked * 3 liked #:: like(received) } } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 |
n = gets.to_i total_liked = 0 day_start_people = 5 for i in 0...n do day_liked = day_start_people / 2 received = day_liked * 3 total_liked = total_liked + day_liked day_start_people = received end puts total_liked |