ClarkeRubber

UNSW ProgComp: Problem 1 - 2008

Jun 11th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. <?php
  2.  
  3. $input = <<<END
  4. 10
  5. 10 750
  6. 1.8 0
  7. 3.5 0
  8. 10 0
  9. 10 71.428571428571428571428571428571
  10. 5.5 1000
  11. 10 1220
  12. 16.5 500
  13. 22 600
  14. 26 1000
  15. END;
  16.  
  17. $input = explode("\n", $input);
  18. array_shift($input);
  19.  
  20. foreach($input as $key => $value){
  21.     $input[$key] = explode(" ", $value);
  22. }
  23. //distance km, ascention m
  24.  
  25. function roundnum($num, $nearest = 5){
  26.     return round($num / $nearest) * $nearest;
  27. }
  28.  
  29. function rule_times($d, $a){
  30.     $time1 = $d/3.5 + $a/500;
  31.     if($time1 > 3){
  32.         $time2 = 0.02*pow($time1, 2) + 1.19*$time1 - 0.75;
  33.     }else{
  34.         $time2 = $time1;
  35.     }
  36.  
  37.     //round to the nearest 5
  38.     $time1_min = str_pad(roundnum(($time1 - floor($time1))*60), 2, '0', STR_PAD_LEFT);
  39.     $time2_min = str_pad(roundnum(($time2 - floor($time2))*60), 2, '0', STR_PAD_LEFT);
  40.  
  41.     $time1 = floor($time1).':'.$time1_min;
  42.     $time2 = floor($time2).':'.$time2_min;
  43.  
  44.     return array($time1, $time2);
  45. }
  46.  
  47. foreach($input as $key => $value){
  48.     $times = rule_times($value[0], $value[1]);
  49.     $d = str_pad(number_format($value[0], 1, '.', '').'km', 7, ' ', STR_PAD_LEFT);
  50.     $a = str_pad(number_format($value[1], 0, '.', '').'m', 7, ' ', STR_PAD_LEFT);
  51.     $n_time = str_pad($times[0], 6, ' ', STR_PAD_LEFT);
  52.     $a_time = str_pad($times[1], 6, ' ', STR_PAD_LEFT);
  53.  
  54.     echo 'Distance:'.$d.'  ascent:'.$a.'  Naismith:'.$n_time.'  adjusted time:'.$a_time."\n";
  55. }
Advertisement
Add Comment
Please, Sign In to add comment