Pagini recente » Cod sursa (job #2447538) | Cod sursa (job #2987418) | Cod sursa (job #1178057) | Cod sursa (job #109461) | Cod sursa (job #2278015)
#include<fstream>
#include<math.h>
#include<string.h>
using namespace std;
ifstream f("adapost2.in") ;
ofstream g("adapost2.out") ;
double ax[50001] , ay[50001] , x , y ,sum , sumx ;
int n ;
int sx[] = { -1 , 1 , 0 , 0 } , sy[] = { 0 , 0 , -1 , 1 } ;
double distanta( double bx , double by , double cx , double cy )
{
return (double)sqrt(( bx - cx ) * ( bx - cx ) + ( by - cy ) * ( by - cy )) ;
}
double suma( double bx , double by )
{ double s = 0 ;
for( int i = 1 ; i <= n ; i ++ )
s = s + distanta( bx , by , ax[i] , ay[i] ) ;
return s ;
}
int main(){
f >> n ;
for(int i = 1 ; i <= n ; i ++ )
{
f >> ax[i] >> ay[i] ;
x = x + ax[i] ;
y = y + ay[i] ;
}
x = x / n ;
y = y / n ;
sum = suma( x , y ) ;// g<< sum <<"\n";
double x1 = x , y1 = y ;
double l = 1000 ;
while ( l > 0.0001 ){
int ok = 1 ;
for(int i = 0 ; i <= 3 ; i ++ )
{
x1 = x + l * sx[i] ;
y1 = y + l * sy[i] ;
sumx = suma( x1 , y1 ) ;
// g<< sumx << " \n";
if ( sumx < sum )
{
sum = sumx ;
x = x1 ;
y = y1 ;
ok = 0 ;
// g<< x << " " << y << "\n" ;
}
}
if( ok )
l = l / 2.0 ;
}
g<< x << " " << y ;
}