(백준) 11930 – 가장 작은 경계 구(C++)

쉬운 목차

문제

11930: 가장 작은 둘러싸는 구체(acmicpc.net)

11930: 가장 작은 둘러싸는 구체

단일 양의 정수 N이 첫 번째 줄에 지정됩니다. (1 ≤ N ≤ 1,000) 두 번째 줄부터 N 줄까지 각 점의 좌표를 나타내는 세 개의 정수 x, y, z가 주어진다. x, y, z는 -1,000,000보다 크거나 같고 1,000,000보다 큼

www.acmicpc.net

설명

자세한 설명은 아래 포스팅을 참고하세요.

(백준) 2389 – 세상의 중심에서… (C++) — GreenGroup

(백준) 2389 – 세상의 중심에서… (C++)

연습 2389: In the center of the world… (acmicpc.net) 연습 2389: In the center of the world… N(1≤N≤100)이 첫 번째 줄에 지정됩니다. 다음 N 줄은 x, y 좌표를 제공합니다. 각 좌표는 소수점 이하 6자리로 표시되며,

greengroup.co.kr

#include <iostream>
#include <cmath>
#include <numeric>
using namespace std;
int a(1001), b(1001), c(1001);

int main() {
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> a(i) >> b(i) >> c(i);
    }

    double x = accumulate(a, a + N, 0.0) / N;
    double y = accumulate(b, b + N, 0.0) / N;
    double z = accumulate(c, c + N, 0.0) / N;

    double ratio = 0.1, max_distance, current_distance;
    for (int i = 0; i < 70000; i++) {
        int max_distance_index = 0;
        max_distance = hypot(x - a(0), y - b(0), z - c(0));
        for (int j = 1; j < N; j++) {
            current_distance = hypot(x - a(j), y - b(j), z - c(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;
        z += (c(max_distance_index) - z) * ratio;
        ratio *= 0.999;
    }

    if (round(x) == 0) x = 0;
    if (round(y) == 0) y = 0;
    if (round(z) == 0) z = 0;

    cout.precision(2);
    cout << fixed;
    cout << max_distance;

    return 0;
}