Pagini recente » Cod sursa (job #2565197) | Cod sursa (job #1296539) | Cod sursa (job #133700) | Cod sursa (job #2372392) | Cod sursa (job #2302443)
#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, n;
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
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( pozx[i], pozy[i], xbun, ybun);
return sum;
}
int main()
{
int i;
double copiex, copiey;
f >> n;
for(i = 1; i <= n; i++)
{
f >> pozx[i] >> pozy[i];
xbun += pozx[i];
ybun += pozy[i];
}
// centru de greutate
xbun = xbun *1.0 / n;
ybun = ybun *1.0 / n;
dis = suma(xbun, ybun);
while (pas > 0.0001)
{
int verf = 0;
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;
verf = 1;
}
}
if(verf == 0)
pas /= 2;
}
g << xbun<< " " << ybun;
return 0;
}