Advertisement
edward4324

laba po yampu 18

Mar 30th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.20 KB | None | 0 0
  1. //задача 18
  2.  
  3. #include <iostream>
  4. #include <Windows.h>
  5. #include <string>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. bool
  10. condition_a(const int& N, vector<vector<int>>& T, vector<vector<int>>* unsafe_int);
  11. bool
  12. condition_a(vector<vector<int>>* T);
  13. vector<vector<int>>
  14. condition_B(vector<vector<int>>* T, vector<vector<int>>* unsafe_int, const int EndTime, const int Length);
  15. vector<vector<int>>*
  16. condiotion_G(const vector<vector<int>>& T, const vector<vector<int>>* unsafe_int, const int EndTime, const int Length);
  17.  
  18. int main() {
  19.  
  20.     SetConsoleCP(1251);
  21.     SetConsoleOutputCP(1251);
  22.  
  23.     vector<vector<int>>* T = new vector<vector<int>>;
  24.     T->resize(2);
  25.  
  26.     int N;
  27.  
  28.     cout << "Введите количество сторожей: ";
  29.     cin >> N;
  30.  
  31.     int input;
  32.  
  33.     for (int i = 0; i < N; i++) {
  34.  
  35.         cout << "Введите интервалы для сторожа №:" << i + 1 << endl;
  36.         cin >> input;
  37.         T->at(0).push_back(input);
  38.         cin >> input;
  39.         T->at(1).push_back(input);
  40.         system("cls");
  41.     }
  42.  
  43.     int Length,
  44.         EndTime;
  45.  
  46.     cout << "Введите продолжительность дежурства дополнительных сторожей: ";
  47.     cin >> Length;
  48.  
  49.     cout << "Введите время окончания дежурства сторожей: ";
  50.     cin >> EndTime;
  51.  
  52.     vector<vector<int>>* uns_periods = new vector<vector<int>>;
  53.     uns_periods->resize(3);
  54.     uns_periods->at(2).push_back(0);
  55.  
  56.     bool proverka = condition_a(N, *T, uns_periods);
  57.  
  58.     if (proverka) {
  59.  
  60.         cout << "Да" << endl;
  61.  
  62.         cout << "Расписание сторожей: " << endl;
  63.  
  64.         for (int i = 0; i < T->at(0).size(); i++) {
  65.  
  66.             cout << "Сторож №" << i + 1 << "\n\tНачало смены: " << T->at(0)[i] << "\tКонец смены: " << T->at(1)[i] << "\t\n";
  67.  
  68.         }
  69.            
  70.  
  71.     }
  72.     else {
  73.  
  74.         cout << "Нет" << endl;
  75.  
  76.         cout << "Информация о проблемных временных промежутках:" << endl;
  77.         Sleep(50);
  78.         system("cls");
  79.         //ПРИ ВЫЗОВЕ ЭТОЙ ФУНКЦИИ НЕ ИЗМЕНЯЕТСЯ Т - ПОФИКСИТЬ
  80.         *T = condition_B(T, uns_periods, EndTime, Length);
  81.  
  82.         cout << "Информация о дополнительных сторожах:";
  83.  
  84.         int k = uns_periods->at(0).size();
  85.        
  86.         for (int i = 0; i < k; i++)
  87.             cout << "\nПромежуток №" << i + 1 << "\tНачало смены: " << T->at(0).at(i + k) << "\tКонец смены: " << T->at(1).at(i + k) << "\n"
  88.             << "\tКоличество сторожей:" << uns_periods->at(2).at(i);
  89.  
  90.  
  91.  
  92.     }
  93.  
  94.     return 0;
  95.  
  96. }
  97.  
  98. //условие a
  99. bool
  100. condition_a(const int& N, vector<vector<int>>& T, vector<vector<int>>* unsafe_int) {
  101.  
  102.     bool result = false;
  103.     int count = 0;
  104.  
  105.  
  106.     for (int i = 0; i < N; i++) {
  107.  
  108.         for (int j = 0; j < N; j++) {
  109.  
  110.             if (T[0][i] >= T[0][j] && T[1][i] <= T[1][j])
  111.                 //T[0][i] >= T[0][j] && T[1][i] >= T[1][j]
  112.                 count++;
  113.  
  114.         }
  115.  
  116.             if (count >= 2)
  117.                 result = true;
  118.             else {
  119.                
  120.  
  121.                 result = false;
  122.                 unsafe_int->at(0).push_back(T[0][i]);
  123.                 unsafe_int->at(1).push_back(T[1][i]);
  124.                 if
  125.                 (unsafe_int->at(2).at(0) == 0) {
  126.  
  127.                     unsafe_int->at(2).at(0) = (count);
  128.  
  129.                 }
  130.  
  131.                 else
  132.  
  133.                 unsafe_int->at(2).push_back(count);
  134.  
  135.             }
  136.  
  137.        count = 0;
  138.  
  139.     }
  140.  
  141.     return result;
  142.  
  143. }
  144.  
  145. //перегрузка для того, чтобы можно было проверять работу условия в пункте В
  146. bool
  147. condition_a(vector<vector<int>>* T) {
  148.  
  149.     bool result = false;
  150.     int count = 0;
  151.  
  152.  
  153.     for (int i = 0; i < T->at(0).size(); i++) {
  154.  
  155.         for (int j = 0; j < T->at(0).size(); j++) {
  156.  
  157.             if (
  158.  
  159.                 T->at(0).at(i) >= T->at(0).at(j) && T->at(1).at(i) <= T->at(1).at(j)
  160.  
  161.                 )
  162.                 count++;
  163.  
  164.         }
  165.  
  166.         if (count < 2)
  167.             return false;
  168.         else
  169.             result = true;
  170.  
  171.         count = 0;
  172.  
  173.     }
  174.  
  175.     return result;
  176.  
  177. }
  178.  
  179. //условие б
  180. vector<vector<int>>
  181. condition_B(vector<vector<int>>* T, vector<vector<int>>* unsafe_int, const int EndTime, const int Length) {
  182.  
  183.     vector<vector<int>>* temp = new vector<vector<int>>;
  184.     *temp = *T;
  185.     int i = 0;
  186.     int N = T->size();
  187.  
  188.     while (i < unsafe_int->size()) {
  189.  
  190.         if (!condition_a(temp)) {
  191.  
  192.             //чтобы выполнить пункт в выходных данных нужно ещё добавить вектор,
  193.             /*который будет сохранять промежутки времени у дополнительных сторожей*/
  194.  
  195.             if (unsafe_int->at(0).at(i) + Length <= EndTime) {
  196.  
  197.                 temp->at(0).push_back(unsafe_int->at(0).at(i));
  198.                 temp->at(1).push_back(unsafe_int->at(0).at(i) + Length);
  199.  
  200.             }
  201.             else if ((unsafe_int->at(1).at(i) - Length <= EndTime) && (unsafe_int->at(1).at(i) <= EndTime)) {
  202.  
  203.                 temp->at(0).push_back(unsafe_int->at(1).at(i) - Length);
  204.                 temp->at(1).push_back(unsafe_int->at(1).at(i));
  205.  
  206.             }
  207.         }
  208.  
  209.         i++;
  210.  
  211.     }
  212.  
  213.     return *temp;
  214.  
  215. }
  216.  
  217. //условие г (объединённое с д ибо задание без объединения не имеет смысла))
  218. vector<vector<int>>*
  219. condiotion_G(const vector<vector<int>>& T, const vector<vector<int>>* unsafe_int, const int EndTime, const int Length) {
  220.  
  221.     /*if (T[0].size() * Length < EndTime)
  222.         return nullptr;*/
  223.  
  224.     vector<vector<int>>* temp;
  225.  
  226.     temp = new vector<vector<int>>;
  227.    
  228.     int diff;
  229.     bool found = false;
  230.  
  231.     //создаем двумерный вектор для хранения информации о том
  232.     //какое количество временных промежутков работы сторожей есть
  233.     for (int i = 0; i < T.at(0).size(); i++)
  234.     {
  235.  
  236.         diff = T[1][i] - T[0][i];
  237.  
  238.         if (temp == nullptr) {
  239.             //если временный вектор пустой, то создаем в памяти новый
  240.             temp = new vector<vector<int>>;
  241.             //первая строка будет отвечать за время на одну смены, а вторая за количество таких смен
  242.             temp->resize(2);
  243.  
  244.             temp->at(0).push_back(diff);
  245.             temp->at(1).push_back(1);
  246.         }
  247.         else {
  248.             //если вектор содержит в себе элементы, то делаем следующее
  249.             int j = 0;
  250.             //пока счетчик не дойдёт до последнего элемента вектора
  251.             while(j < temp->at(0).size()){
  252.                 //если найдется аналогичное время смены
  253.                 if (temp->at(0).at(j) == diff) {
  254.                     //увеличиваем счетчик у данного времени
  255.                     temp->at(1).at(j)++;
  256.                     //устанавливаем маркер о том, что нашли время
  257.                     found = true;
  258.                     //приравниваем j к номеру последнего элемента, чтобы цикл закончил работу раньше времени
  259.                     j = temp->at(0).size();
  260.                 }
  261.                 j++;
  262.             }
  263.             //если время не нашли, то добавляем в конец новое время и ставим маркер что время не найденно
  264.             //для возврата к изначальным условиям
  265.             if (!found) {
  266.                 temp->at(0).push_back(diff);
  267.                 temp->at(1).push_back(1);
  268.                 found = false;
  269.             }
  270.         }
  271.     }
  272.      
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement