Pagini recente » Cod sursa (job #957169) | Cod sursa (job #3295403) | Cod sursa (job #2764895) | Cod sursa (job #1757644) | Cod sursa (job #2082777)
#include <fstream>
#include <math.h>
#define nmax 50002
#define err 0.001
using namespace std;
ifstream f("adapost2.in");
ofstream g("adapost2.out");
struct punct{
double x,y;
} p[nmax];
double pas=100;
int n;
double distanta(punct a, punct b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double suma_distante(punct x)
{
double s=0;
for(int i=1;i<=n;++i)
s+=distanta(x,p[i]);
return s;
}
punct CDG()
{
punct cdg;
double x=0,y=0;
for(int i=1;i<=n;++i)
{
x+=p[i].x;
y+=p[i].y;
}
cdg.x=x/(double)n;
cdg.y=y/(double)n;
return cdg;
}
punct gaseste_loc(punct x)
{
if(pas<err) return x;
double dist_curenta=suma_distante(x);
punct vecin;
vecin.x=x.x;
vecin.y=x.y+pas;
if(vecin.y<=1000)
if(suma_distante(vecin)<dist_curenta)
return gaseste_loc(vecin);
vecin.x=x.x;
vecin.y=x.y-pas;
if(vecin.y>=0)
if(suma_distante(vecin)<dist_curenta)
return gaseste_loc(vecin);
vecin.x=x.x+pas;
vecin.y=x.y;
if(vecin.x<=1000)
if(suma_distante(vecin)<dist_curenta)
return gaseste_loc(vecin);
vecin.x=x.x-pas;
vecin.y=x.y;
if(vecin.x>=0)
if(suma_distante(vecin)<dist_curenta)
return gaseste_loc(vecin);
pas/=2;
return gaseste_loc(x);
}
int main()
{
f>>n;
for(int i=1;i<=n;++i)
{
f>>p[i].x;
f>>p[i].y;
}
punct cdg=CDG();
punct loc=gaseste_loc(cdg);
g<<loc.x<<' '<<loc.y;
return 0;
}