Problem
Problem URL : 숫자 카드2
[1] Answer Code (18. 09. 26)
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(void) {
int n;
scanf("%d", &n);
vector<int> v;
for (int i = 0; i < n; i++) {
int num;
scanf("%d", &num);
v.push_back(num);
}
sort(v.begin(), v.end());
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int num;
scanf("%d", &num);
if (binary_search(v.begin(), v.end(), num) == 0) {
printf("0 ");
}
else{
printf("%ld ", upper_bound(v.begin(), v.end(), num) - lower_bound(v.begin(), v.end(), num));
}
}
}
Review
- upper_bound 와 Lower_bound 함수에 대해 모르겠으면 Upper_bound vs Lower_bound을 참고하자.