fork download
  1. #include <bits/stdc++.h>
  2. #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
  3. #define fi first
  4. #define se second
  5. #define el "\n"
  6. #define pb push_back
  7. #define sz(a) (int)(a).size()
  8. #define FILL(a,x) memset(a,x,sizeof(a))
  9. using namespace std;
  10. typedef long long ll;
  11.  
  12. struct Point {
  13. long double x, y, z;
  14. };
  15.  
  16. long double dist3d(const Point &a, const Point &b){
  17. long double dx = a.x - b.x;
  18. long double dy = a.y - b.y;
  19. long double dz = a.z - b.z;
  20. return sqrtl(dx*dx + dy*dy + dz*dz);
  21. }
  22.  
  23. int main(){
  24. ios::sync_with_stdio(false);
  25. cin.tie(nullptr); cout.tie(nullptr);
  26.  
  27. int n;
  28. if(!(cin >> n)) return 0;
  29. vector<Point> p(n);
  30. FOR(i,0,n-1){
  31. cin >> p[i].x >> p[i].y >> p[i].z;
  32. }
  33.  
  34. Point cur;
  35. cur.x = cur.y = cur.z = 0.0L;
  36. FOR(i,0,n-1){
  37. cur.x += p[i].x;
  38. cur.y += p[i].y;
  39. cur.z += p[i].z;
  40. }
  41. cur.x /= (long double)n;
  42. cur.y /= (long double)n;
  43. cur.z /= (long double)n;
  44.  
  45. const long double EPS = 1e-12L;
  46.  
  47. for(int it = 0; it < 10000; ++it){
  48. long double numx = 0.0L, numy = 0.0L, numz = 0.0L, denom = 0.0L;
  49. bool atPoint = false;
  50.  
  51. FOR(i,0,n-1){
  52. long double d = dist3d(cur, p[i]);
  53. if(d < EPS){
  54. cur = p[i];
  55. atPoint = true;
  56. break;
  57. }
  58. numx += p[i].x / d;
  59. numy += p[i].y / d;
  60. numz += p[i].z / d;
  61. denom += 1.0L / d;
  62. }
  63.  
  64. if(atPoint) break;
  65.  
  66. Point nxt;
  67. nxt.x = numx / denom;
  68. nxt.y = numy / denom;
  69. nxt.z = numz / denom;
  70.  
  71. long double move = dist3d(cur, nxt);
  72. cur = nxt;
  73. if(move < EPS) break;
  74. }
  75.  
  76. cout.setf(ios::fixed);
  77. cout << setprecision(4) << (double)cur.x << ' ' << (double)cur.y << ' ' << (double)cur.z;
  78.  
  79. return 0;
  80.  
  81. }
  82.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty