Pagini recente » Cod sursa (job #1030964) | Cod sursa (job #1247386) | Cod sursa (job #3178740) | Cod sursa (job #1279582) | Cod sursa (job #2280951)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("adapost2.in");
ofstream g("adapost2.out");
double pozx[50001], pozy[50001], xbun, ybun, dis, disnew, pas = 1000;
int n, dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, 1, -1};
double distanta(double x1, double y1, double x2, double y2)
{
return sqrt((x1 - x2) * (x1- x2) + (y1 - y2) * (y1 - y2));
}
double suma(double xbun, double ybun)
{
double sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + distanta(xbun, ybun, pozx[i], pozy[i]);
return sum;
}
int main()
{
int i;
f >> n;
for(i = 1; i <= n; i++)
{
f >> pozx[i] >> pozy[i];
xbun += pozx[i];
ybun += pozy[i];
}
xbun = xbun *1.0 / n;
ybun = ybun *1.0 / n;
dis = suma(xbun, ybun);
double copiex, copiey;
while (pas > 0.0001)
{
for (int i = 0; i <= 3; i++)
{
copiex = xbun + pas * dx[i];
copiey = ybun + pas * dy[i];
disnew = suma(copiex, copiey);
if (disnew < dis)
{
dis = disnew;
xbun = copiex;
ybun = copiey;
}
}
pas /= 2;
}
g << xbun<< " " << ybun;
return 0;
}