Advertisement
deusthorr

How to discover a # btwn 1 to 100 using QSort method - PHP

Oct 29th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.36 KB | None | 0 0
  1. for($i = 1; $i <= 100; $i++){
  2. $valor = $i;
  3.  
  4. $tentativas = 0;
  5.  
  6. $try = 0;
  7. $min = 0;
  8. $max = 100;
  9.  
  10. while($valor != $try){
  11.     $tentativas++;
  12.     $try = round(($max - $min) / 2) + $min;
  13.     if($try < $valor){
  14.         $min = $try;
  15.     }elseif($try  > $valor){
  16.         $max = $try;
  17.     }
  18. }
  19.  
  20. echo "Numero $valor Descoberto em $tentativas tentativas.<br> ";
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement