Advertisement
allia

поиск количества вхождений

Oct 6th, 2020
1,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void poisk (int *arr,  int znach, int razmer)
  5. {
  6.   int t=-1;
  7.   int result = 0;
  8.   int right = razmer-1;
  9.   int left = 0;
  10.  
  11.   while (right>=left)
  12.   {
  13.     int i = (right+left)/2;
  14.     int middle = arr[i];
  15.  
  16.      if (middle == znach)
  17.       {
  18.        t=i;
  19.        while (arr[t]==znach && t!=-1)
  20.        t--;
  21.        result = t+1;
  22.        t+=1;
  23.        while (arr[t]==znach && t!=razmer)
  24.        t++;
  25.        result = t-result;
  26.        cout << result << endl;
  27.        goto printl;
  28.       }
  29.      else if (znach < middle)
  30.         right = i-1;
  31.      else if (znach > middle)
  32.         left = i+1;
  33.   }
  34.  
  35. printl:
  36. if (t==-1)
  37. cout << "Not found" << endl;
  38. }
  39.  
  40. int main()
  41. {
  42.   int n=0, m=0, *arr;
  43.   cin >> n;
  44.  
  45.   arr = new int[n];
  46.  for (int i=0; i<n; i++)
  47.   {
  48.     cin >> arr[i];
  49.   }
  50.  
  51.   cin >> m;
  52.  
  53.   int zapros = 0;
  54.  
  55.   for (int j=0; j<m; j++)
  56.   {
  57.     cin >> zapros;
  58.  
  59.     if (zapros < arr[0] || zapros > arr[n-1])
  60.      cout << "Not found" << endl;
  61.     else
  62.      poisk (arr, zapros, n);
  63.  }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement