Pagini recente » Cod sursa (job #2571029) | Cod sursa (job #1177398) | Cod sursa (job #2054805) | Cod sursa (job #619128) | Cod sursa (job #60993)
Cod sursa(job #60993)
#include <fstream.h>
#include <math.h>
#include <iomanip.h>
struct
{
double x, y;
}e[50000];
long int n;
double S;
double X, Y;
void citire ( )
{
ifstream f ("adapost2.in");
f>>n;
long int i;
for (i=1; i<=n; i++)
f>>e[i].x>>e[i].y;
f.close();
}
int main ( )
{
citire ( );
int t=1, p=0;
double xa=0, ya=0, s=0, k=10;
long int i;
for (i=1; i<=n; i++)
{
xa+=e[i].x;
ya+=e[i].y;
}
xa/=n;
ya/=n;
for (i=1; i<=n; i++)
S+=sqrt(pow(e[i].x-xa, 2)+pow(e[i].y-ya, 2));
while (k>0.00001)
{
t=1;
p=0;
while (t)
{
t=0;
s=0;
for (i=1; i<=n&&s<S; i++)
s+=sqrt(pow(e[i].x-(xa+k), 2)+pow(e[i].y-ya, 2));
if (s<S)
{
xa+=k;
S=s;
X=xa;
Y=ya;
t=1;
p=1;
}
}
t=1;
while (t)
{
t=0;
s=0;
for (i=1; i<=n&&s<S; i++)
s+=sqrt(pow(e[i].x-(xa-k), 2)+pow(e[i].y-ya, 2));
if (s<S)
{
xa-=k;
S=s;
X=xa;
Y=ya;
t=1;
p=1;
}
}
t=1;
while (t)
{
t=0;
s=0;
for (i=1; i<=n&&s<S; i++)
s+=sqrt(pow(e[i].x-xa, 2)+pow(e[i].y-(ya+k), 2));
if (s<S)
{
ya+=k;
S=s;
X=xa;
Y=ya;
t=1;
p=1;
}
}
t=1;
while (t)
{
t=0;
s=0;
for (i=1; i<=n&&s<S; i++)
s+=sqrt(pow(e[i].x-xa, 2)+pow(e[i].y-(ya-k), 2));
if (s<S)
{
ya-=k;
S=s;
X=xa;
Y=ya;
t=1;
p=1;
}
}
if (!p)
k/=10;
}
ofstream g ("adapost2.out");
g<<X<<" "<<Y;
g.close();
return 0;
}