[BaekJoon] #14681 - 사분면 고르기 [Java][C++]
[BaekJoon] #14681 - 사분면 고르기 [Java][C++]
1. 아이디어
$x$, $y$의 부호로 위치한 사분면을 판단할 수 있어서 조건문을 활용하면 해결할 수 있다.
- 1사분면의 경우 $x > 0$, $y > 0$
- 2사분면의 경우 $x < 0$, $y > 0$
- 3사분면의 경우 $x < 0$, $y < 0$
- 4사분면의 경우 $x > 0$, $y < 0$
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
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());
int y = Integer.parseInt(br.readLine());
if (x > 0) {
if (y > 0) {
System.out.println(1);
} else {
System.out.println(4);
}
} else {
if (y > 0) {
System.out.println(2);
} else {
System.out.println(3);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int x, y;
cin >> x >> y;
if (x > 0) {
if (y > 0) {
cout << 1;
} else {
cout << 4;
}
} else {
if (y > 0) {
cout << 2;
} else {
cout << 3;
}
}
}
This post is licensed under CC BY 4.0 by the author.