Pagini recente » Cod sursa (job #1713286) | Cod sursa (job #1807358) | Cod sursa (job #680625) | Cod sursa (job #2507627) | Cod sursa (job #814758)
Cod sursa(job #814758)
#include <cstdio>
#include <cmath>
#include <iostream>
#include <fstream>
#define eps 0.0001
using namespace std;
ifstream input("adapost2.in");
ofstream output("adapost2.out");
double myDistance ( int x1, int y1, int x2, int y2 ){
return sqrt( (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1-y2) );
}
int N;
double a[50001];
double b[50001];
double newx = 0.0, newy = 0.0;
double oldx = 0.0, oldy = 0.0;
int main(){
input >> N;
for( int i = 0 ; i < N; i++ ){
double x , y;
input >> x >> y;
a[i] = x ; b[i] = y;
oldx += x; oldy += y;
}
oldx /= N;
oldy /= N;
double weight = 0.0;
double val;
while(1){
for( int i = 0 ; i < N; i++ ){
val = myDistance( a[i], b[i], oldx, oldy );
weight += 1/val;
newx += a[i]/val ;
newy += b[i]/val;
}
newx = newx/weight;
newy = newy/weight;
weight = 0;
if( myDistance( newx, newy, oldx, oldy ) < eps )
break;
oldx = newx;
oldy = newy;
newx = newy = 0;
}
output<< newx<<" "<<newy;
input.close();
output.close();
return 0;
}