Average or EMPTY
PHP
Medium
6 views
Problem Description
Given n numbers, print average with 2 decimals. If n=0, print EMPTY.
Input Format
First n. Next line n numbers.
Output Format
Average or EMPTY.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$tokens=preg_split('/\\s+/', $inputText);
$i=0; $n=intval($tokens[$i++] ?? 0);
if($n===0){ echo 'EMPTY'; exit; }
$sum=0.0;
for($k=0;$k<$n;$k++) $sum+=floatval($tokens[$i++] ?? 0);
echo number_format($sum/$n,2,'.','');
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!