Clamp a Number
PHP
Easy
4 views
Problem Description
Input x lo hi. Print x clamped inside [lo,hi].
Input Format
One line: x lo hi.
Output Format
One integer.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
[$x,$lo,$hi]=array_map('intval',preg_split('/\\s+/', $inputText));
if($x<$lo) $x=$lo;
if($x>$hi) $x=$hi;
echo $x;
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!