Petro_zzz

quick_search

Dec 13th, 2023
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. // https://en.wikipedia.org/wiki/Rostov_Oblast
  7.  
  8.  
  9. const char* text = R"(Residents identified
  10. themselves as belonging to 157 different
  11. ethnic groups, including 27 of more than 2,000
  12. persons each. The largest ethnicities are[14]
  13. the 3,795,607 Russians (90.3%);
  14. the 110,727 Armenians (2.6%) and the 77,802
  15. Ukrainians (1.9%). Other important groups
  16. are the 35,902 Turks (0.9%); 16,493 Belarusians (0.4%);
  17. 13,948 Tatars (0.3%); 17,961 Azerbaijanis (0.4%); 11,449
  18. Chechens (0.3%); 16,657 Romani (0.4%); 11,597 Koreans (0.3%);
  19. 8,296 Georgians (0.2%), and 2,040 Assyrians (.05%).
  20. There were also 76,498 people (1.8%) belonging to
  21. other ethno-cultural groupings. 76,735 people were
  22. registered from administrative databases, and could
  23. not declare an ethnicity. It is estimated that the proportion
  24. of ethnicities in this group is the same as that of the declared
  25. group.[21])";
  26.  
  27.  
  28. void my_copy(char* des, int n, const char* src) {
  29.     for (int k = 0; k < n; k++)
  30.         des[k] = src[k];
  31. }
  32.  
  33.  
  34. int parse(const char* text, double*& out) {
  35.     int num = 0;
  36.    
  37.    
  38.     char* buff = new char[10];
  39.  
  40.     int len = strlen(text);
  41.     cout << "len: " << len << endl;
  42.     int bg = 0;
  43.     int ed = 0;
  44.     for (int k = 0; k < len; k++) {
  45.         if (text[k] == '(')
  46.             bg = k+1;
  47.         if (text[k] == ')') {
  48.             ed = k-1;
  49.             my_copy(buff, ed-bg, &(text[bg]));
  50.             buff[ed - bg] = '\0';          
  51.             out[num++] = atof(buff);           
  52.         }
  53.     }
  54.     delete[] buff;
  55.     return num;
  56. }
  57.  
  58.  
  59. void task1() {
  60.     char buff[10];
  61.     auto out = new double[100];
  62.     int num = parse(text, out);
  63.     for (int k = 0; k < num; k++)
  64.         cout << out[k] << ", ";
  65.     cout << endl;
  66.     delete[] out;
  67.     /*
  68.     for (int k = 0, n = 10; ((n > 0) && (k < 10)); k++, n--)
  69.         cout << k << " " << n << endl;
  70.     int data[5][5]{};
  71.     for (int r = 0; r < 5; r++) {
  72.         data[2][r] = 15 + r;
  73.         data[4][r] = 15 - r;
  74.     }
  75.     for (int r = 0; r < 5; r++) {
  76.         cout << data[2][r] - data[4][r];
  77.     }
  78.     */
  79. }
  80.  
  81. int binary_search_recursion(int* arr, int left, int right, int target) {
  82.     if (left <= right) {
  83.         int midle = left + (right - left) / 2;     
  84.         if (arr[midle] == target) {        
  85.             return midle;
  86.         }
  87.         else if (arr[midle] > target) {        
  88.             binary_search_recursion(arr,left, midle-1, target);
  89.         }
  90.         else {
  91.             binary_search_recursion(arr,midle+1, right, target);
  92.         }
  93.     } else  
  94.         return -1;
  95. }
  96.  
  97. int binary_search(int* arr, int left, int right, int target) {
  98.     while (left <= right) {
  99.         int midle = left + (right - left) / 2;
  100.         if (arr[midle] == target) {
  101.             return midle;
  102.         }
  103.         else if (arr[midle] > target) {
  104.             right = midle - 1;
  105.         }
  106.         else {
  107.             left = midle + 1;
  108.         }
  109.     }
  110.     return -1;
  111. }
  112.  
  113. void test_binary_search() {
  114.     int arr[]{1,2,3,4,5,6,7,8,9};
  115.     cout << "7 at " << binary_search(arr,0,size(arr),7) << endl;
  116. }
  117.  
  118. int main() {
  119.     test_binary_search();
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment