Advertisement
Luxeon94

17/18 Q2: Counting in Sorted Vectors; X84777

Nov 15th, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<int> read_vector(int n) {
  6.    vector<int> v(n);
  7.    // add your code here
  8.    // ...
  9.    for(int i=0; i<n; ++i) cin >> v[i];
  10.    return v;
  11. }
  12.  
  13. int count_elements(const vector<int>& v, int x) {
  14.    // add your code here
  15.     int count=0;
  16.     for(int i=0; i<v.size(); ++i){
  17.         if(v[i]==x) ++count;
  18.     }
  19.     return count;
  20. }
  21.  
  22. int main() {
  23.    int n;
  24.    while (cin >> n) {
  25.       vector<int> v = read_vector(n);
  26.       int nqueries;
  27.       cin >> nqueries;
  28.       for (int i =0; i < nqueries; ++i) {
  29.          int x;
  30.          cin >> x;
  31.          cout << count_elements(v,x) << endl;
  32.       }
  33.    }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement