Created
September 28, 2018 07:08
-
-
Save flymio/9d77e05b80a436546d6d8c2aba6c3b38 to your computer and use it in GitHub Desktop.
Solution for https://www.hackerrank.com/challenges/plus-minus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Из чужих решений узнал про функцию bcdiv: | |
string bcdiv ( string $dividend , string $divisor [, int $scale = 0 ] ) | |
Операция деления для чисел произвольной точности |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Complete the plusMinus function below. | |
function plusMinus($arr) { | |
$plus = 0; | |
$minus = 0; | |
$zero = 0; | |
$n = count($arr); | |
foreach($arr as $value){ | |
if ($value>0){ | |
$plus++; | |
} | |
else if ($value<0){ | |
$minus++; | |
} | |
else{ | |
$zero++; | |
} | |
} | |
printf("%0.06f\n",$plus/$n); | |
printf("%0.06f\n",$minus/$n); | |
printf("%0.06f\n",$zero/$n); | |
} | |
$stdin = fopen("php://stdin", "r"); | |
fscanf($stdin, "%d\n", $n); | |
fscanf($stdin, "%[^\n]", $arr_temp); | |
$arr = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY)); | |
plusMinus($arr); | |
fclose($stdin); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment