Answer to Question #322343 in C++ for Abhik

Question #322343

A race is going to be held soon in college. Each racer will be assigned 3 random points from a set of N distinct points, the ith point is denoted by (Xi,Yi). In order to complete the race, each person has to run on a triangular track, assuming the 3 assigned points to be the corners of the track.

Raghav being the lazy participant wanted to run on the track with the least distance among all the possible tracks. Now, he wants your help to find out what is the smallest distance he has to run in order to finish the race.


Input

The first line contains an integer N(3≤N≤100)

N(3≤N≤100) — the number of points.

This is followed by N lines, each containing two integer Xi and Yi (−10^9≤Xi,Yi≤10^9)

— the coordinates of the ith point.


Output

Print a single real number d where d is the perimeter of the smallest track.


1
Expert's answer
2022-06-28T08:19:31-0400
#include <iostream>

using namespace std;

double distance(long x1, long y1, long x2, long y2)
{
	return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

int main()
{
	int n;
	cin >> n;
	long* x = new long[n];
	long* y = new long[n];
	for (int i = 0; i < n; i++)
	{
		cin >> x[i];
		cin >> y[i];
	}
	double minP = DBL_MAX;
	for (int i = 0; i < n - 2; i++)
		for (int j = i + 1; j < n - 1; j++)
			for (int k = j + 1; k < n; k++)
			{
				double p = 0;
				p += distance(x[i], y[i], x[j], y[j]);
				p += distance(x[i], y[i], x[k], y[k]);
				p += distance(x[j], y[j], x[k], y[k]);
				if (p < minP)
					minP = p;
			}
	//cout.precision(3); // precision of output was not specified; if specific precision is needed then uncomment this line and replace 3 with the needed precision
	cout << minP;
	delete[] x;
	delete[] y;
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog