We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
A scala solution: the idea is to create a list of pairs of consecutive points using zip
objectSolution{defdist(p1:Array[Double],p2:Array[Double]):Double={math.sqrt(math.pow(p1(0)-p2(0),2)+math.pow(p1(1)-p2(1),2))}defmain(args:Array[String]){valread=()=>scala.io.StdIn.readLine// the list of all pointsvalpointList=(1toread().toInt).map(_=>read().split(" ").map(_.toDouble)).toList// first part is the list of pairs of consecutive pointsvalperimeter=(pointListzip(pointList.last::pointList.init)).map{case(p1,p2)=>dist(p1,p2)}.sumprintln(perimeter)}}
Compute the Perimeter of a Polygon
You are viewing a single comment's thread. Return to all comments →
A scala solution: the idea is to create a list of pairs of consecutive points using
zip