Pagini recente » Cod sursa (job #2897560) | Cod sursa (job #3150624) | Monitorul de evaluare | Cod sursa (job #3149804) | Cod sursa (job #1054491)
#include <iostream>
#include <fstream>
#include <math.h>
#include <iomanip>
const static int NMAX = 50002;
using namespace std;
class Point
{
public:
double x, y;
Point(const double & x = 0, const double & y = 0)
{
this->x = x;
this->y = y;
}
double distanceToPoint(const Point & other) const
{
return sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
}
};
ifstream input("adapost2.in");
ofstream output("adapost2.out");
Point puncte[NMAX];
int N;
double sumDist(const Point & point)
{
double sum = 0;
for (int i = 0; i < N; i++)
{
sum += point.distanceToPoint(puncte[i]);
}
return sum;
}
double round(double x)
{
return floor(x * 10000 + 0.5) / 10000;
}
int main()
{
Point aux;
Point punct;
Point intermediar;
input >> N ;
for (int i = 0; i < N ; i++)
{
input >> puncte[i].x >> puncte[i].y;
punct.x += puncte[i].x;
punct.y += puncte[i].y;
}
punct.x /= N;
punct.y /= N;
double zecimale = 200;
int dirx[] = { 0 , 0 , -1 , 1};
int diry[] = { -1 , 1 , 0 , 0};
double min_dist = sumDist(punct);
while (zecimale > 0.0001)
{
bool found = false;
for (int i =0 ; i < 4; i++)
{
intermediar = Point(punct.x + dirx[i] * zecimale , punct.y + diry[i] * zecimale);
double dist = sumDist(intermediar);
if (min_dist > dist)
{
min_dist = dist;
aux = intermediar;
found = true;
}
}
if (found)
{
punct = aux;
aux = Point(0,0);
}
else
{
zecimale /= 2;
}
}
output << setprecision(5) << punct.x << " " << punct.y << endl;
return 0;
}