본문 바로가기
Computer Science

A응실 균형점 / 구현

by OKOK 2018. 12. 17.

1. 완전 탐색이랑 구현이랑 비슷한 말임

2. 그냥 구현 하면 됨

3. 문제 대로 꼬아서 구현하지 말고

4. 최적화는 다음 문제

5. 예제 2개 돌려서 확실하게 로직 파악


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
double x[10], mass[10];
double left = 0, right = 0;
int i, N;
 
int main() {
    freopen("input.txt""r", stdin);
    int T, n;
    scanf("%d"&T);
 
    for (int t = 1; t <= T; t++) {
        printf("#%d", t);
        scanf("%d"&N);
        for (i = 0; i < N; i++) {
            scanf("%d"&n);
            x[i] = n;
        }
        for (i = 0; i < N; i++) {
            scanf("%d"&n);
            mass[i] = n;
        }
        for (i = 0; i < N - 1; i++) {
            double start = x[i], end = x[i + 1];
            while (start<end) {
                double balance = (start + end/ 2.0;
                left = 0, right = 0;
                for (int j = 0; j <= i; j++) {
                    double d = balance - x[j];
                    left += mass[j] / (d*d);
                }
                for (int j = i + 1; j <= N - 1; j++) {
                    double d = balance - x[j];
                    right += mass[j] / (d*d);
                }
                if (left > right) start = balance; /// 오른쪽으로 더 가야될때
                else end = balance; /// 왼쪽으로 더 가야할때
                double z = balance - (start + end/ 2.0;
                if (z < 0)
                    z *= -1;
                if (z <= 0.0000000000001) {
                    printf(" %.10lf", balance);
                    break;
                }
            }
        }
        printf("\n");
    }
    return 0;
}
 
cs



댓글