File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ // 값이 [left_value, right_value]인 데이터의 개수를 반환하는 함수
6+ int countByRange (vector<int > v, int leftValue, int rightValue) {
7+ vector<int >::iterator rightIndex = upper_bound (v.begin (), v.end (), rightValue);
8+ vector<int >::iterator leftIndex = lower_bound (v.begin (), v.end (), leftValue);
9+ return rightIndex - leftIndex;
10+ }
11+
12+ int n, x;
13+ vector<int > v;
14+
15+ int main () {
16+ // 데이터의 개수 N, 찾고자 하는 값 x 입력받기
17+ cin >> n >> x;
18+
19+ // 전체 데이터 입력 받기
20+ for (int i = 0 ; i < n; i++) {
21+ int temp;
22+ cin >> temp;
23+ v.push_back (temp);
24+ }
25+
26+ // 값이 [x, x] 범위에 있는 데이터의 개수 계산
27+ int cnt = countByRange (v, x, x);
28+
29+ // 값이 x인 원소가 존재하지 않는다면
30+ if (cnt == 0 ) {
31+ cout << -1 << ' \n ' ;
32+ }
33+ // 값이 x인 원소가 존재한다면
34+ else {
35+ cout << cnt << ' \n ' ;
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments