Post

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

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

문제 링크


1. 아이디어

주어진 성냥이 박스에 들어갈 수 있는지 판단하는 문제로 직사각형 박스에서 가장 긴 변인 대각선의 길이보다 성냥의 길이가 짧거나 같으면 들어갈 수 있다. 피타고라스 정리를 활용하면 간단하게 구할 수 있다.


2. 복잡도

접근시간공간
풀이$O(N)$$O(1)$

($N$ = 입력값 n)


3. 코드

풀이 [Java][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
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());

        while (n-- > 0) {
            int x = Integer.parseInt(br.readLine());

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

        bw.flush();
    }
}
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;

    while (n--) {
        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.