Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- read N
- touch list # create file named list
- for (( i=0; i<N; i++ )); do
- read Pi # store in Pi
- echo $Pi >> list # append to file list
- done
- #cat List : list >& 2
- sort -n list > sorted # sort file list and create file sorted
- #echo Sorted : >& 2
- #cat sorted >& 2
- minimum=99999999
- distance=0
- previous=0
- first=$(head -n 1 sorted) # first horse from sorted
- #echo head : $first >& 2
- for i in $(cat sorted); do # iterate through lines from sorted
- #echo i : $i >& 2
- if [ $i != $first ]; then
- #echo -e '\t' first : $previous >& 2
- #echo -e '\t' second : $i >&2
- let "distance=i-previous" # calculate distance
- #echo -e '\t' "$distance = $i-$previous" >& 2
- # Since list is sorted i know that distance is always >= 0
- #echo -e '\t' distance : $distance >& 2
- if [ $distance -lt $minimum ]; then
- let "minimum=distance" # store minimal distance
- fi
- let "previous=i" # current i horse becomes previous
- else
- let "previous=i" # first horse becomes previous
- fi
- done
- echo "$minimum" # print minimum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement