You are viewing a single comment's thread. Return to all comments →
Scala:
import scala.io.StdIn._ import scala.math.sqrt import scala.math.pow object Solution { def main(args: Array[String]) { val n = readInt val points = (1 to n).map{ i => val line = readLine.split(" ") line(0).toInt -> line(1).toInt }.toSeq //println(points) println(getPerimeter(points)) } def getPerimeter(points: Seq[(Int, Int)]): Double = { (0 until points.size-1).map { i => val p1 = points(i) val p2 = points(i+1) calculateDistance(p1, p2) } .toSeq .sum + calculateDistance(points(0), points(points.size-1)) } private def calculateDistance(p1:(Int, Int), p2: (Int, Int)): Double = sqrt(pow((p1._1 - p2._1),2) + pow((p1._2 - p2._2),2)) }
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: