Advertisement
Guest User

Horse-Racing Duals BASH NC

a guest
Nov 6th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.86 KB | None | 0 0
  1. read N
  2. touch list      # create file named list
  3. for (( i=0; i<N; i++ )); do
  4.     read Pi     # store in Pi
  5.     echo $Pi >> list    # append to file list
  6. done
  7. sort -n list > sorted   # sort file list and create file sorted
  8.  
  9. minimum=99999999
  10. distance=0
  11. previous=0
  12. first=$(head -n 1 sorted)   # first horse from sorted
  13.  
  14. for i in $(cat sorted); do  # iterate through lines from sorted
  15.     if [ $i != $first ]; then
  16.         let "distance=i-previous"   # calculate distance        
  17.         # Since list is sorted i know that distance is always >= 0        
  18.         if [ $distance -lt $minimum ]; then    
  19.             let "minimum=distance"      # store minimal distance
  20.         fi
  21.         let "previous=i"    # current i horse becomes previous
  22.     else
  23.         let "previous=i"    # first horse becomes previous
  24.     fi
  25.  done
  26.  
  27. echo "$minimum"     # print minimum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement