Post

[BaekJoon] 3034번 - 앵그리 창영 [Java][C++]

[BaekJoon] 3034번 - 앵그리 창영 [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
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));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int n = Integer.parseInt(st.nextToken());
        int w = Integer.parseInt(st.nextToken());
        int h = Integer.parseInt(st.nextToken());

        for (int i = 0; i < n; i++) {
            int x = Integer.parseInt(br.readLine());

            if (x * x <= w * w + h * h) {
                bw.write("DA\n");
            } else {
                bw.write("NE\n");
            }
        }

        bw.flush();
    }
}


2. 풀이 [C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;

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

    int n, w, h;
    cin >> n >> w >> h;

    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;

        if (x * x <= w * w + h * h) {
            cout << "DA\n";
        } else {
            cout << "NE\n";
        }
    }
}

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