백준 문제풀기/조건문

[Java] 백준 14681 사분면 고르기

psys 2022. 11. 14. 14:13
728x90

문제

https://www.acmicpc.net/problem/14681

 

14681번: 사분면 고르기

점 (x, y)의 사분면 번호(1, 2, 3, 4 중 하나)를 출력한다.

www.acmicpc.net

 

풀이

이건 가독성이 떨어진단다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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());
		
		int result = x>0&&y>0?1:x>0&&y<0?4:x<0&&y<0?3:2;
		System.out.println(result);
		
	}
}

 

다시 풀었다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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());
		int result = 0;
		
		if(x>0&&y>0) {
			result=1;
		} else if(x<0&&y>0) {
			result=2;
		} else if(x<0&&y<0) {
			result=3;
		} else {
			result=4;
		}
		
		System.out.println(result);
		
	}
}

'백준 문제풀기 > 조건문' 카테고리의 다른 글

[Java] 백준 2480 주사위 세개  (0) 2022.11.14
[Java] 백준 2525 오븐 시계  (0) 2022.11.14
[Java] 백준 2884 알람 시계  (0) 2022.11.14
[Java] 백준 2753 윤년  (0) 2022.11.14