[백준] 14681번 - 사분면 고르기 [Java][C++]
[백준] 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. 코드
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());
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);
}
}
}
}
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
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;
}
}
}
3. 풀이 정보
1. 구현 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 100 ms | 14116 KB | 617 B |
2. 구현 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 0 ms | 2020 KB | 376 B |
This post is licensed under CC BY 4.0 by the author.