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 findArea pairs = let constructMatrix pairs = let (firstPair, rest) = match pairs with | x::xs -> x, xs | _ -> failwith "Wrong" pairs @ [firstPair] let rec calc pairs acc = match pairs with | (x1, y1)::(x2, y2)::rest -> let (a1, a2) = acc let acc' = (a1 + (x1 * y2), a2 + (x2 * y1)) calc ((x2, y2)::rest) acc' | _ -> let (a1, a2) = acc float ((abs (a1 - a2))) / 2.0 let matrix = constructMatrix pairs calc matrix (0, 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 |> findArea |> (printfn "%A") runTestCase()
Compute the Area of a Polygon
You are viewing a single comment's thread. Return to all comments →