Post

[백준] 2756번 - 다트 [Java][C++]

[백준] 2756번 - 다트 [Java][C++]

문제 링크


1. 문제 풀이

다트가 다트판에서 몇 점에 위치하는지 구할 수 있어야 하는 문제로 다트판의 중심과 다트와의 거리를 피타고라스 정리로 구하고 해당 거리가 어떤 고리에 위치하는지 판단하면 다트의 점수를 구할 수 있다.


2. 코드

1. 구현 [Java]

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
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st;

        int T = Integer.parseInt(br.readLine());
        for (int tc = 1; tc <= T; tc++) {
            int p1 = 0;
            int p2 = 0;

            st = new StringTokenizer(br.readLine());
            p1 += getPoint(dist(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())));
            p1 += getPoint(dist(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())));
            p1 += getPoint(dist(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())));
            p2 += getPoint(dist(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())));
            p2 += getPoint(dist(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())));
            p2 += getPoint(dist(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())));

            if (p1 > p2) {
                sb.append("SCORE: ").append(p1).append(" to ").append(p2).append(", PLAYER 1 WINS.\n");
            } else if (p1 < p2) {
                sb.append("SCORE: ").append(p1).append(" to ").append(p2).append(", PLAYER 2 WINS.\n");
            } else {
                sb.append("SCORE: ").append(p1).append(" to ").append(p2).append(", TIE.\n");
            }
        }

        System.out.println(sb);
    }

    static double dist(double x, double y) {
        return Math.sqrt(x * x + y * y);
    }

    static int getPoint(double dist) {
        if (dist <= 3) return 100;
        if (dist <= 6) return 80;
        if (dist <= 9) return 60;
        if (dist <= 12) return 40;
        if (dist <= 15) return 20;
        return 0;
    }
}

2. 구현 [C++]

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
#include <bits/stdc++.h>
using namespace std;

int point(double dist) {
    if (dist <= 3) return 100;
    if (dist <= 6) return 80;
    if (dist <= 9) return 60;
    if (dist <= 12) return 40;
    if (dist <= 15) return 20;
    return 0;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    for (int tc = 1; tc <= t; tc++) {
        int p1 = 0;
        int p2 = 0;

        double x, y;
        for (int i = 0; i < 3; i++) {
            cin >> x >> y;
            p1 += point(sqrt(x * x + y * y));
        }
        for (int i = 0; i < 3; i++) {
            cin >> x >> y;
            p2 += point(sqrt(x * x + y * y));
        }

        if (p1 > p2) {
            cout << "SCORE: " << p1 << " to " << p2 << ", PLAYER 1 WINS.\n";
        } else if (p1 < p2) {
            cout << "SCORE: " << p1 << " to " << p2 << ", PLAYER 2 WINS.\n";
        } else {
            cout << "SCORE: " << p1 << " to " << p2 << ", TIE.\n";
        }
    }
}

3. 풀이 정보

1. 구현 [Java]

언어시간메모리코드 길이
Java 11176 ms19604 KB1891 B

2. 구현 [C++]

언어시간메모리코드 길이
C++ 174 ms2032 KB989 B

This post is licensed under CC BY 4.0 by the author.