Post

[BaekJoon] 9610번 - 사분면 [Java][C++]

[BaekJoon] 9610번 - 사분면 [Java][C++]

문제 링크


1. 문제 풀이


축 위에 있으면 $x$ 또는 $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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int[] cnt = new int[5];

        int n = Integer.parseInt(br.readLine());
        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());

            if (x == 0 || y == 0) {
                cnt[0]++;
                continue;
            }

            if (x > 0) {
                if (y > 0) {
                    cnt[1]++;
                } else {
                    cnt[4]++;
                }
            } else {
                if (y > 0) {
                    cnt[2]++;
                } else {
                    cnt[3]++;
                }
            }
        }

        System.out.println("Q1: " + cnt[1]);
        System.out.println("Q2: " + cnt[2]);
        System.out.println("Q3: " + cnt[3]);
        System.out.println("Q4: " + cnt[4]);
        System.out.println("AXIS: " + cnt[0]);
    }
}


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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    vector<int> cnt(5);

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;

        if (x == 0 || y == 0) {
            cnt[0]++;
            continue;
        }

        if (x > 0) {
            if (y > 0) {
                cnt[1]++;
            } else {
                cnt[4]++;
            }
        } else {
            if (y > 0) {
                cnt[2]++;
            } else {
                cnt[3]++;
            }
        }
    }

    cout << "Q1: " << cnt[1] << '\n';
    cout << "Q2: " << cnt[2] << '\n';
    cout << "Q3: " << cnt[3] << '\n';
    cout << "Q4: " << cnt[4] << '\n';
    cout << "AXIS: " << cnt[0] << '\n';
}

This post is licensed under CC BY 4.0 by the author.