[BaekJoon] 30676번 - 이 별은 무슨 색일까 [Java][C++]
[BaekJoon] 30676번 - 이 별은 무슨 색일까 [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
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");
}
}
}
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int x;
cin >> x;
if (x >= 620) {
cout << "Red\n";
} else if (x >= 590) {
cout << "Orange\n";
} else if (x >= 570) {
cout << "Yellow\n";
} else if (x >= 495) {
cout << "Green\n";
} else if (x >= 450) {
cout << "Blue\n";
} else if (x >= 425) {
cout << "Indigo\n";
} else {
cout << "Violet\n";
}
}
This post is licensed under CC BY 4.0 by the author.