Post

[BaekJoon] #30676 - 이 별은 무슨 색일까 [Java][C++]

[BaekJoon] #30676 - 이 별은 무슨 색일까 [Java][C++]

문제 링크


1. 아이디어

조건문을 활용해서 큰 파장부터 판별하는 방식으로 조건 분기를 했다.


2. 복잡도

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

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

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

        int x = Integer.parseInt(br.readLine());

        if (x >= 620) {
            System.out.println("Red");
        } else if (x >= 590) {
            System.out.println("Orange");
        } else if (x >= 570) {
            System.out.println("Yellow");
        } else if (x >= 495) {
            System.out.println("Green");
        } else if (x >= 450) {
            System.out.println("Blue");
        } else if (x >= 425) {
            System.out.println("Indigo");
        } else {
            System.out.println("Violet");
        }
    }
}
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
#include <bits/stdc++.h>
using namespace std;

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

    int x;
    cin >> x;

    if (x >= 620) {
        cout << "Red";
    } else if (x >= 590) {
        cout << "Orange";
    } else if (x >= 570) {
        cout << "Yellow";
    } else if (x >= 495) {
        cout << "Green";
    } else if (x >= 450) {
        cout << "Blue";
    } else if (x >= 425) {
        cout << "Indigo";
    } else {
        cout << "Violet";
    }
}

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