질문
2626: 헬리콥터
문제를 단순화하기 위해 섬의 크기는 무시하고 섬의 위치는 2차원 정수 좌표로 표현한다.
첫 번째 줄은 섬의 수를 나타내는 정수 N(2≤N≤1000)입니다.
다음 N 라인은 섬의 각 행의 x 좌표입니다.
www.acmicpc.net
설명하다
자세한 지침은 아래 게시물을 참조하십시오.
(백준) 2389 – 세상의 중심에서…(C++) — GreenGroup
(백준) 2389 – 세상의 중심에서… (C++)
문제 2389: 세계의 중심에서… (acmicpc.net) 질문 2389: 세계의 중심에서… 첫 번째 줄은 N(1≤N≤100)을 나타냅니다.
다음 N 줄은 x,y 좌표를 제공합니다.
각 좌표는 소수점 이하 6자리,
그린그룹.co.kr
#include <iostream>
#include <cmath>
#include <numeric>
using namespace std;
int a(1001), b(1001);
int main() {
int N;
cin >> N;
for(int i = 0; i < N; i++){
cin >> a(i) >> b(i);
}
double x = accumulate(a, a + N, 0.0) / N;
double y = accumulate(b, b + N, 0.0) / N;
double ratio = 0.1, max_distance, current_distance;
for (int i = 0; i < 30000; i++) {
int max_distance_index = 0;
max_distance = hypot(x - a(0), y - b(0));
for (int j = 1; j < N; j++) {
current_distance = hypot(x - a(j), y - b(j));
if (max_distance < current_distance) {
max_distance = current_distance;
max_distance_index = j;
}
}
x += (a(max_distance_index) - x) * ratio;
y += (b(max_distance_index) - y) * ratio;
ratio *= 0.994;
}
if (round(x) == 0) x = 0;
if (round(y) == 0) y = 0;
cout.precision(3);
cout << fixed;
cout << x << " " << y << "\n";
cout << max_distance;
return 0;
}