You are viewing a single comment's thread. Return to all comments →
object Solution { def distance(p1:(Int,Int),p2:(Int,Int)): Double = math.sqrt(math.pow(p1._1-p2._1,2)+math.pow(p1._2-p2._2,2)) def main(args: Array[String]) { var lines=io.Source.stdin.getLines().drop(1) val points = lines.toList.map(s =>s.split(" ")).map {case Array(e1, e2) => (e1.toInt, e2.toInt)} val sliding = points.sliding(2).collect { case List(x, y) => (x, y) }.toList val allEdges = sliding :+ (points.last -> points.head) val perimeter = allEdges.map {case (p1,p2) => distance(p1, p2)} println(perimeter.sum) } }
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 →
Scala solution: