Post

[BaekJoon] 10807번 - 개수 세기 [Java][C++]

[BaekJoon] 10807번 - 개수 세기 [Java][C++]

문제 링크


1. 아이디어


N개의 정수와 정수 v가 주어졌을 때, N개의 정수 중에 v가 몇 개 포함됐는지 구하는 간단한 문제다.


2. 코드


1. 풀이 [Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 n = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine());
        int v = Integer.parseInt(br.readLine());
        int cnt = 0;

        while (n-- > 0) {
            int x = Integer.parseInt(st.nextToken());
            if (x == v) cnt++;
        }

        System.out.println(cnt);
    }
}


2. 풀이 [C++]

count 함수를 활용해 벡터의 전체 구간에서 개수를 세줬다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;

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

    int n;
    cin >> n;

    vector<int> v(n);
    for (int& x : v) cin >> x;

    int x;
    cin >> x;
    cout << count(v.begin(), v.end(), x);
}

3. 디버깅


없음.


4. 참고


없음.


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