gimmickbutreal

[백준/자바] 14681번 해설 - Java 본문

Algorithm/Java

[백준/자바] 14681번 해설 - Java

isshosng 2022. 1. 16. 11:21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 예를 들어, 좌표가 (12, 5)인 점 A는 x좌표와 y좌표가 모두 양수이므로 제1사분면에 속한다. 점 B는 x좌표가 음수이고 y좌표가 양수이므로 제2사분면에 속한다.
// 점의 좌표를 입력받아 그 점이 어느 사분면에 속하는지 알아내는 프로그램을 작성하시오. 단, x좌표와 y좌표는 모두 양수나 음수라고 가정한다.
 
// 첫 줄에는 정수 x가 주어진다. (−1000 ≤ x ≤ 1000; x ≠ 0) 다음 줄에는 정수 y가 주어진다. (−1000 ≤ y ≤ 1000; y ≠ 0)
 
// 점 (x, y)의 사분면 번호(1, 2, 3, 4 중 하나)를 출력한다.
import java.util.Scanner;
public class Main {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int x = in.nextInt();
        int y = in.nextInt();
in.close();
        
        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(4);        
                }
            }
        }
    }
cs

tip)

- if문 { } 활용하기

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// BufferedReader는 객체 생성시 생성자의 입력값으로 InputStreamReader의 객체가 필요함
// InputStreamReader - character / BufferedReader - String
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());
        // readLine method를 쓸 때 InputStream==null일 경우 오류가 생기므로 예외처리를 함
        // readLine : String 값으로 개행 문자를 포함하여 읽음
        
        if(X > 0) {
            if(Y > 0) {
                System.out.print(1);
            } 
            else {
                System.out.print(4);
            }
        } 
        else {
            if(Y > 0) {
                System.out.print(2);
            } 
            else {
                System.out.print(3);
            }
        }
    }
}
cs

'Algorithm > Java' 카테고리의 다른 글

[백준/자바] 2739번 - Java  (0) 2022.01.18
[백준/자바] 2884번 - Java  (2) 2022.01.17
[백준/자바] 2753번 해설 - Java  (0) 2022.01.15
[백준/자바] 9498번 해설 - Java  (0) 2022.01.14
[백준/자바] - 1330번 Java  (0) 2022.01.13