Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. //#include <pch.h>
  2. #include <iostream>
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <math.h>
  7. #include <cmath>
  8. #include <malloc.h>
  9. #include <iomanip>
  10. #include <clocale>
  11. using namespace std;
  12. HANDLE hout;
  13. int BinarnuiPoisk(int** arr, int stroki, int stolb, int iskomui, int* inum)
  14. {
  15.     int j = 0;
  16.     int first, last, mid, temp;
  17.     for (int i = 0; i < stroki; i++) {
  18.         first = 0;
  19.         last = stolb;
  20.         while (first < last) {
  21.             mid = first + (last - first) / 2;
  22.             if (iskomui <= arr[i][mid])
  23.                 last = mid;
  24.             else
  25.                 first = mid + 1;
  26.         }
  27.         if (arr[i][last] == iskomui) {
  28.             *inum = ++j;
  29.             temp = last;
  30.             while (arr[i][--last] == iskomui && last > 0)
  31.                 *inum = ++j;
  32.                     while (arr[i][++temp] == iskomui && temp < stolb)
  33.                     *inum = ++j;
  34.                     goto stop;
  35.         }
  36.         else {
  37.             goto stop;
  38.         }
  39.     stop:;
  40.     }
  41.     if (j != 0) return 0;
  42.     else return -1;
  43. }
  44. void insertSort(int* arr, int arr_size)
  45. {
  46.     int tmp;
  47.     for (int i = 1, j; i < arr_size; ++i)
  48.     {
  49.         tmp = arr[i];
  50.         for (j = i - 1; j >= 0; j -= 1)
  51.         {
  52.             if (arr[j] > tmp)
  53.             {
  54.                 arr[j + 1] = arr[j];
  55.             }
  56.             else break;
  57.         }
  58.         if ((j + 1) != i)
  59.             arr[j + 1] = tmp; // вставить проверяемый элемент в найденную позицию
  60.     }
  61. }
  62. int main()
  63. {
  64.     setlocale(LC_ALL, "Russian");
  65.     HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  66.     CONSOLE_SCREEN_BUFFER_INFO s_a;
  67.     GetConsoleScreenBufferInfo(hOut, &s_a);
  68.     hout = GetStdHandle(STD_OUTPUT_HANDLE);
  69.     int M = 0;
  70.     int N = 0;
  71.     cout << "Задана матрица размером M х N" << endl;
  72.     cout << "Введите кол-во столбцов M: ";
  73.     cin >> M;
  74.     cout << "Введите кол-во строк N: ";
  75.     cin >> N;
  76.     cout << endl;
  77.     if (((M < 1) || (double(M) - int(M) != 0)) || ((N < 1) || (double(N) - int(N) != 0)))
  78.     {
  79.         cout << "Пожалуйста, введите значения правильные!!!";
  80.         system("pause");
  81.         return 0;
  82.     }
  83.     else
  84.     {
  85.         //ЗАДАЕМ МАССИВ
  86.         int** matr;
  87.         matr = new int* [N];
  88.         for (int i = 0; i < N; i++)
  89.             matr[i] = new int[M];
  90.         cout << "МАССИВ: ";
  91.         int i, j;
  92.         srand(time(0));
  93.         for (i = 0; i < N; i++)
  94.         {
  95.             cout << endl;
  96.             for (j = 0; j < M; j++)
  97.             {
  98.                 matr[i][j] = 1+ rand() % 10;
  99.                 printf("%6i", matr[i][j]);
  100.             }
  101.         }
  102.         cout << endl;
  103.         cout << endl;
  104.         cout << "Задание: Произвести поиск элемента(последовательности элементов) по четным строкам и нечетным столбцам матрицы." << endl;
  105.             cout << endl;
  106.         int dlinaa;
  107.         cout << "Последовательность какой длины Вы хотите найти? ";
  108.         cin >> dlinaa;
  109.         if ((dlinaa < 1) || (double(dlinaa) - int(dlinaa) != 0))
  110.         {
  111.             cout << "Вы ввели недопустимое значение!";
  112.             system("pause");
  113.             return 0;
  114.         }
  115.         cout << "Введите элементы последовательного поиска: ";
  116.         int* posl = new int[dlinaa];
  117.         for (int i = 0; i < dlinaa; i++) {
  118.             cin >> posl[i];
  119.         }
  120.         int** prov; //проверочный массив
  121.         prov = new int* [N];
  122.         for (int i = 0; i < N; i++)
  123.             prov[i] = new int[M];
  124.         for (i = 0; i < N; i++)
  125.         {
  126.             for (j = 0; j < M; j++)
  127.             {
  128.                 prov[i][j] = 0;
  129.             }
  130.         }
  131.         int sov = 0;
  132.         int check1, check2;
  133.         for (int i = 0; i < N; i++) { //ПОИСК ПО ЧЕТНЫМ СТРОКАМ
  134.             if (i % 2 != 0) {
  135.                 for (int j = 0; j < M; j++) {
  136.                     if (matr[i][j] == posl[0] && (M - j) >= dlinaa) {
  137.                         check1 = 0; check2 = 0;
  138.                         for (int s = 0; s < dlinaa; s++) {
  139.                             if (matr[i][j + s] != posl[s])
  140.                             {
  141.                                 check1++;
  142.                                 if (prov[i][j + s] != 0)
  143.                                     check2++;
  144.                             }
  145.                         }
  146.                         if (check1 == 0 && check2 < dlinaa) {
  147.                             sov++;
  148.                             for (int s = 0; s < dlinaa; s++) {
  149.                                 prov[i][j + s] = 1;
  150.                             }
  151.                         }
  152.                     }
  153.                 }
  154.             }
  155.         }
  156.         for (int j = 0; j < M; j++) { //ПОИСК ПО НЕЧЕТНЫМ СТОЛБЦАМ
  157.             if (j % 2 != 1) {
  158.                 for (int i = 0; i < N; i++) {
  159.                     if (matr[i][j] == posl[0] && (N - i) >= dlinaa) {
  160.                         check1 = 0; check2 = 0;
  161.                         for (int s = 0; s < dlinaa; s++) {
  162.                             if (matr[i + s][j] != posl[s])
  163.                             {
  164.                                 check1++;
  165.                                 if (prov[i + s][j] != 0)
  166.                                     check2++;
  167.                             }
  168.                         }
  169.                         if (check1 == 0 && check2 < dlinaa) {
  170.                             sov++;
  171.                             for (int s = 0; s < dlinaa; s++) {
  172.                                 prov[i + s][j] = 1;
  173.                             }
  174.                         }
  175.                     }
  176.                 }
  177.             }
  178.         }
  179.         int sum = 0; //Сумма пров массива
  180.         for (i = 0; i < N; i++)
  181.         {
  182.             for (j = 0; j < M; j++)
  183.             {
  184.                 sum = sum + prov[i][j];
  185.             }
  186.         }
  187.         cout << endl;
  188.         cout << "Количество совпадений: ";
  189.         if (dlinaa == 1)
  190.             cout << sum;
  191.         else cout << sov;
  192.         for (int i = 0; i < N; i++) {
  193.             for (int j = 0; j < M; j++) {
  194.                 prov[i][j] = 0;
  195.             }
  196.         }
  197.         cout << endl << endl;
  198.         cout << "Бинарный поиск:" << endl << endl;
  199.         for (i = 0; i < N; i++)
  200.         {
  201.             for (j = 0; j < M; j++)
  202.             {
  203.             }
  204.         }
  205.         int c;
  206.         cout << "Введите элемент бинарного поиска: ";
  207.         cin >> c;
  208.         cout << endl;
  209.         for (i = 0; i < N; i = i + 2)
  210.         {
  211.             for (j = 1; j < M; j = j + 2)
  212.             {
  213.                 matr[i][j] = -1;
  214.             }
  215.         }
  216.         for (int i = 0; i < N; ++i)
  217.         {
  218.             insertSort(matr[i], M);
  219.         }
  220.         int number = 0;
  221.         int* inum = &number;
  222.         if (BinarnuiPoisk(matr, N, M, c, &number) != -1)
  223.         {
  224.             cout << "Искомый элемент есть в матрице" << endl;
  225.             cout << "Количество совпадений: " << number << endl;
  226.         }
  227.         else cout << "Искомый элемент не найден" << endl;
  228.         delete(matr);
  229.         delete(prov);
  230.         return 0;
  231.         system("pause");
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement