[Programmers] #120841 - 점의 위치 구하기 [Java][C++][Python]
[Programmers] #120841 - 점의 위치 구하기 [Java][C++][Python]
1. 아이디어
점 $(x, y)$가 몇 사분면에 속하는지 구하는 문제다. 좌표가 0이 아니어서 축 위의 점이 없으므로 사분면은 두 좌표의 부호만으로 정해진다. $x$가 양수이면 $y$가 양수일 때 1사분면, 음수일 때 4사분면이고, $x$가 음수이면 $y$가 양수일 때 2사분면, 음수일 때 3사분면이므로 $x$의 부호를 먼저 가른 뒤 $y$의 부호를 가르면 된다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int solution(int[] dot) {
int x = dot[0], y = dot[1];
if (x > 0) {
if (y > 0) {
return 1;
} else {
return 4;
}
} else {
if (y > 0) {
return 2;
} else {
return 3;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> dot) {
int x = dot[0], y = dot[1];
if (x > 0) {
if (y > 0) {
return 1;
} else {
return 4;
}
} else {
if (y > 0) {
return 2;
} else {
return 3;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
def solution(dot):
x, y = dot
if x > 0:
if y > 0:
return 1
else:
return 4
else:
if y > 0:
return 2
else:
return 3
This post is licensed under CC BY 4.0 by the author.