https://www.acmicpc.net/problem/14681
14681번: 사분면 고르기
점 (x, y)의 사분면 번호(1, 2, 3, 4 중 하나)를 출력한다.
www.acmicpc.net
문제
import java.util.Scanner;
public class Main14681 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
if (x>=0) {
if (y>=0) {
System.out.println(1);
} else if(y<0) {
System.out.println(4);
}
} else if (x<0) {
if (y>=0) {
System.out.println(2);
} else if(y<0) {
System.out.println(3);
}
}
sc.close();
}
}
이전 문제와는 다르게 논리연산자를 쓰지않고 중첩if문으로 문제해결을 했다. 성능에 대한 차이는 아직 잘 모르겠으나 육안으로 볼 경우 코드가 한눈에 들어오는 차이가 있다. 여러번 연습하며 차이점을 숙지해보자.
'백준' 카테고리의 다른 글
백준 2480번 220906 (0) | 2022.09.06 |
---|---|
백준 2884번 220906 (0) | 2022.09.06 |
백준 2753번 220905 (0) | 2022.09.05 |
백준 9498번 220905 (0) | 2022.09.05 |
백준 1330번 220903 (0) | 2022.09.03 |