Pagini recente » Cod sursa (job #1867588) | Cod sursa (job #3196399) | Cod sursa (job #1598264) | Cod sursa (job #1611683) | Cod sursa (job #1054517)
#include <iostream>
#include <stdio.h>
#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));
}
};
Point puncte[NMAX];
int N;
double sumDist(const Point & point)
{
double sum = 0.0;
for (int i = 0; i < N; i++)
{
sum += point.distanceToPoint(puncte[i]);
}
return sum;
}
int main()
{
FILE * input = fopen("adapost2.in" , "r");
FILE * output = fopen("adapost2.out" , "w");
Point punct;
Point intermediar;
fscanf(input , "%d" , &N);
for (int i = 0; i < N ; i++)
{
fscanf(input , "%lf %lf" , &puncte[i].x , &puncte[i].y);
punct.x += puncte[i].x;
punct.y += puncte[i].y;
}
punct.x /= N;
punct.y /= N;
register double zecimale = 300;
register int i;
register bool found;
double dirx[] = { 0.0 , 0.0 , -1.0 , 1.0};
double diry[] = { -1.0 , 1.0 , 0.0 , 0.0};
double min_dist = sumDist(punct);
while (zecimale > 0.0005)
{
found = false;
for (i =0 ; i < 4 && !found; i++)
{
intermediar = Point(punct.x + dirx[i] * zecimale , punct.y + diry[i] * zecimale);
double dist = sumDist(intermediar);
if (min_dist > dist)
{
min_dist = dist;
punct = intermediar;
found = true;
}
}
if (!found)
{
zecimale /= 2.0;
}
}
fprintf(output , "%.4lf %.4lf" , punct.x , punct.y);
return 0;
}