You are viewing a single comment's thread. Return to all comments →
open System let readPair() = match Console.ReadLine().Split([|' '|]) |> Array.map Int32.Parse |> Array.toList with | x::y::[] -> x, y | _ -> failwith "Invalid pair" let readPairs n = let rec readPairs n r = match n with | 0 -> r | _ -> readPairs (n - 1) (readPair()::r) readPairs n [] let dist p1 p2 = let (x1, y1), (x2, y2) = p1, p2 sqrt (((float (x2 - x1)) ** 2.0) + ((float (y2 - y1)) ** 2.0)) let findPerimeter pairs = let rec f firstPair previousPair pairs acc = match previousPair, pairs with | p1, p2::rest -> f firstPair p2 rest (acc + (dist p1 p2)) | _, [] -> acc + (dist previousPair firstPair) match pairs with | p::rest -> f p p rest 0.0 | _ -> failwith "Wrong" let runTestCase() = Console.ReadLine() |> Int32.Parse |> readPairs |> findPerimeter |> (printfn "%A") runTestCase()
Seems like cookies are disabled on this browser, please enable them to open this website
Compute the Perimeter of a Polygon
You are viewing a single comment's thread. Return to all comments →