Advertisement
fatalryuu

laba

Oct 6th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int main() {
  9.     setlocale(LC_ALL, "RUSSIAN");
  10.  
  11.     const int MIN_VALUE_OF_SEGMENTS = 2;
  12.     const int MAX_VALUE_OF_SEGMENTS = 10;
  13.     const int MIN_VALUE_OF_COORD = -100;
  14.     const int MAX_VALUE_OF_COORD = 100;
  15.  
  16.     int n;
  17.     int maxIntersectionValue;
  18.     string maxIntersectionSegments;
  19.     bool isInCorrect;
  20.  
  21.     maxIntersectionValue = 0;
  22.     maxIntersectionSegments = "1";
  23.  
  24.     cout << "Эта программа определяет отрезок, входящий в наибольшее количество пересечений с другими отрезками.\n";
  25.     cout << "Введите количество отрезков от " << MIN_VALUE_OF_SEGMENTS << " до " << MAX_VALUE_OF_SEGMENTS << ": ";
  26.  
  27.     do {
  28.         isInCorrect = false;
  29.         cin >> n;
  30.         if (cin.fail())
  31.  
  32.             isInCorrect = true;
  33.  
  34.         cin.clear();
  35.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  36.         if (isInCorrect) {
  37.            
  38.             cout << "Ошибка! Введите целое число: ";
  39.             isInCorrect = true;
  40.  
  41.         }
  42.         if (!isInCorrect && (n < MIN_VALUE_OF_SEGMENTS || n > MAX_VALUE_OF_SEGMENTS)) {
  43.            
  44.             cout << "Ошибка! Введите число из указанного диапазона: ";
  45.             isInCorrect = true;
  46.  
  47.         }
  48.     } while (isInCorrect);
  49.  
  50.     int* intersections = new int[n] {0};
  51.     int** segments = new int* [n];
  52.     for (int i = 0; i < n; i++)
  53.         segments[i] = new int[2];
  54.  
  55.     for (int i = 0; i < n; i++) {
  56.         cout << "Введите координаты начала и конца " << i + 1 << "-ого отрезка в диапазоне от " << MIN_VALUE_OF_COORD << " до " << MAX_VALUE_OF_COORD << ".\n";
  57.  
  58.         do {
  59.             isInCorrect = false;
  60.             cout << "Введите первую координату: ";
  61.             cin >> segments[i][0];
  62.             if (cin.fail())
  63.                 isInCorrect = true;
  64.             cin.clear();
  65.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  66.             if (isInCorrect)
  67.  
  68.                 cout << "Ошибка! Введите целую координату.";
  69.  
  70.             if (!isInCorrect && (segments[i][0] < MIN_VALUE_OF_COORD || segments[i][0] > MAX_VALUE_OF_COORD)) {
  71.  
  72.                 cout << "Ошибка! Введите координату из указанного диапазона.";
  73.                 isInCorrect = true;
  74.  
  75.             }
  76.         } while (isInCorrect);
  77.  
  78.         do {
  79.             isInCorrect = false;
  80.             cout << "Введите вторую координату: ";
  81.             cin >> segments[i][1];
  82.             if (cin.fail())
  83.                 isInCorrect = true;
  84.             cin.clear();
  85.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  86.             if (isInCorrect)
  87.  
  88.                 cout << "Ошибка! Введите целую координату.";
  89.  
  90.             if (!isInCorrect && (segments[i][1] < MIN_VALUE_OF_COORD || segments[i][1] > MAX_VALUE_OF_COORD)) {
  91.  
  92.                 cout << "Ошибка! Введите координату из указанного диапазона.";
  93.                 isInCorrect = true;
  94.  
  95.             }
  96.             if (!isInCorrect && (segments[i][0] == segments[i][1])) {
  97.  
  98.                 cout << "Введите разные координаты! \n";
  99.                 isInCorrect = true;
  100.  
  101.             }
  102.         } while (isInCorrect);
  103.  
  104.         if (segments[i][0] > segments[i][1]) {
  105.  
  106.             segments[i][0] = segments[i][0] + segments[i][1];
  107.             segments[i][1] = segments[i][0] - segments[i][1];
  108.             segments[i][0] = segments[i][0] - segments[i][1];
  109.  
  110.         }
  111.  
  112.     }
  113.  
  114.     for (int i = 0; i < n; i++) {
  115.         for (int k = 0; k < n; k++) {
  116.             if (k != i) {
  117.                 if (segments[k][0] >= segments[i][0] && segments[k][0] <= segments[i][1] || segments[k][1] >= segments[i][0] && segments[k][1] <= segments[i][1]) {
  118.  
  119.                     intersections[i]++;
  120.  
  121.                     if (segments[k][0] > segments[i][0] && segments[k][1] < segments[i][1]) {
  122.  
  123.                         intersections[k]++;
  124.                     }
  125.  
  126.                 }
  127.  
  128.             }
  129.  
  130.         }
  131.  
  132.     }
  133.  
  134.     for (int i = 1; i < n; i++) {
  135.  
  136.         if (intersections[i] == intersections[maxIntersectionValue]) {
  137.  
  138.             maxIntersectionSegments += ", " + to_string(i + 1);
  139.  
  140.         }
  141.  
  142.         if (intersections[i] > intersections[maxIntersectionValue]) {
  143.  
  144.             maxIntersectionSegments = "";
  145.             maxIntersectionSegments += to_string(i + 1);
  146.             maxIntersectionValue = i;
  147.  
  148.         }
  149.  
  150.     }
  151.  
  152.     if (intersections[maxIntersectionValue] == 0) {
  153.  
  154.         cout << "Отрезки не пересекаются.";
  155.  
  156.     }
  157.  
  158.     else {
  159.  
  160.         cout << "Наибольшее количество пересечений имеет(ют) отрезок(и) " << maxIntersectionSegments << ". Оно равно " << intersections[maxIntersectionValue] << "\n";
  161.  
  162.     }
  163.  
  164.     return 0;
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement