Pagini recente » Cod sursa (job #1021448) | Cod sursa (job #2080915) | Cod sursa (job #1877281) | Cod sursa (job #3246414) | Cod sursa (job #130784)
Cod sursa(job #130784)
#include<stdio.h>
#include<math.h>
FILE*f=fopen("adapost2.in","r");
FILE*g=fopen("adapost2.out","w");
long double a[50002], b[50002], X,Y;
int n;
long double smin;
long double dist(long double x, long double y,long double i,long double j)//distanta dintre 2 pcte
{
return sqrt((i-x)*(i-x)+(j-y)*(j-y));
}
long double suma(long double x, long double y)//suma distantelor de la (x.y) la (a[i],b[i])
{
int i;
long double s=0.0;
for(i=1;i<=n;++i)
s+=dist(x,y,a[i],b[i]);
return s;
}
void read()
{
int i;
fscanf(f,"%d\n",&n);
for(i=1;i<=n;++i) fscanf(f,"%Lf %Lf\n",&a[i],&b[i]);
}
void solve()
{
long double x, y, sc=0.0, pas=1, sa=0.0;
int t=10;
x=y=0.0;
smin=suma(x,y);
X=x; Y=y;
while(t--)
{
//deplasare la dreapta
pas=1;
while(pas>=0.0001)
{
x=x+pas;
sc=suma(x,y);
if(sc<smin) smin=sc,X=x,Y=y;
else
{
x=x-pas;
pas=pas/2;
}
}
//deplasare in sus
pas=1;
while(pas>=0.0001)
{
y=y+pas;
sc=suma(x,y);
if(sc<smin) smin=sc,X=x,Y=y;
else
{
y=y-pas;
pas=pas/2;
}
}
//deplasare la stanga
pas=1;
while(pas>=0.0001)
{
x=x-pas;
sc=suma(x,y);
if(sc<smin) smin=sc,X=x,Y=y;
else
{
x=x+pas;
pas=pas/2;
}
}
//deplasare in jos
pas=1;
while(pas>=0.0001)
{
y=y-pas;
sc=suma(x,y);
if(sc<smin) smin=sc,X=x,Y=y;
else
{
y=y+pas;
pas=pas/2;
}
}
}
fprintf(g,"%.4Lf %.4Lf",X,Y);
}
int main()
{
read();
solve();
return 0;
}