[백준] 10807번 - 개수 세기 [Java][C++]
[백준] 10807번 - 개수 세기 [Java][C++]
1. 문제 풀이
$N$ 개의 정수와 정수 $v$ 가 주어졌을 때, $N$ 개의 정수 중에 $v$ 가 몇 개 포함됐는지 구하는 문제다. 배열을 활용해 $N$ 개의 정수를 입력 받고, 순회하며 비교하여 개수를 세주면 된다.
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
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());
int[] arr = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
int v = Integer.parseInt(br.readLine());
int cnt = 0;
for (int n : arr) {
if (n == 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(false);
cin.tie(nullptr);
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. 풀이 정보
1. 구현 [Java]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| Java 11 | 112 ms | 14240 KB | 650 B |
2. 구현 [C++]
| 언어 | 시간 | 메모리 | 코드 길이 |
|---|---|---|---|
| C++ 17 | 0 ms | 2020 KB | 265 B |
This post is licensed under CC BY 4.0 by the author.