Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- setlocale(LC_ALL, "RUSSIAN");
- const int MIN_VALUE_OF_SEGMENTS = 2;
- const int MAX_VALUE_OF_SEGMENTS = 10;
- const int MIN_VALUE_OF_COORD = -100;
- const int MAX_VALUE_OF_COORD = 100;
- int n;
- int maxIntersectionValue;
- string maxIntersectionSegments;
- bool isInCorrect;
- maxIntersectionValue = 0;
- maxIntersectionSegments = "1";
- cout << "Эта программа определяет отрезок, входящий в наибольшее количество пересечений с другими отрезками.\n";
- cout << "Введите количество отрезков от " << MIN_VALUE_OF_SEGMENTS << " до " << MAX_VALUE_OF_SEGMENTS << ": ";
- do {
- isInCorrect = false;
- cin >> n;
- if (cin.fail())
- isInCorrect = true;
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- if (isInCorrect) {
- cout << "Ошибка! Введите целое число: ";
- isInCorrect = true;
- }
- if (!isInCorrect && (n < MIN_VALUE_OF_SEGMENTS || n > MAX_VALUE_OF_SEGMENTS)) {
- cout << "Ошибка! Введите число из указанного диапазона: ";
- isInCorrect = true;
- }
- } while (isInCorrect);
- int* intersections = new int[n] {0};
- int** segments = new int* [n];
- for (int i = 0; i < n; i++)
- segments[i] = new int[2];
- for (int i = 0; i < n; i++) {
- cout << "Введите координаты начала и конца " << i + 1 << "-ого отрезка в диапазоне от " << MIN_VALUE_OF_COORD << " до " << MAX_VALUE_OF_COORD << ".\n";
- do {
- isInCorrect = false;
- cout << "Введите первую координату: ";
- cin >> segments[i][0];
- if (cin.fail())
- isInCorrect = true;
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- if (isInCorrect)
- cout << "Ошибка! Введите целую координату.";
- if (!isInCorrect && (segments[i][0] < MIN_VALUE_OF_COORD || segments[i][0] > MAX_VALUE_OF_COORD)) {
- cout << "Ошибка! Введите координату из указанного диапазона.";
- isInCorrect = true;
- }
- } while (isInCorrect);
- do {
- isInCorrect = false;
- cout << "Введите вторую координату: ";
- cin >> segments[i][1];
- if (cin.fail())
- isInCorrect = true;
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- if (isInCorrect)
- cout << "Ошибка! Введите целую координату.";
- if (!isInCorrect && (segments[i][1] < MIN_VALUE_OF_COORD || segments[i][1] > MAX_VALUE_OF_COORD)) {
- cout << "Ошибка! Введите координату из указанного диапазона.";
- isInCorrect = true;
- }
- if (!isInCorrect && (segments[i][0] == segments[i][1])) {
- cout << "Введите разные координаты! \n";
- isInCorrect = true;
- }
- } while (isInCorrect);
- if (segments[i][0] > segments[i][1]) {
- segments[i][0] = segments[i][0] + segments[i][1];
- segments[i][1] = segments[i][0] - segments[i][1];
- segments[i][0] = segments[i][0] - segments[i][1];
- }
- }
- for (int i = 0; i < n; i++) {
- for (int k = 0; k < n; k++) {
- if (k != i) {
- 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]) {
- intersections[i]++;
- if (segments[k][0] > segments[i][0] && segments[k][1] < segments[i][1]) {
- intersections[k]++;
- }
- }
- }
- }
- }
- for (int i = 1; i < n; i++) {
- if (intersections[i] == intersections[maxIntersectionValue]) {
- maxIntersectionSegments += ", " + to_string(i + 1);
- }
- if (intersections[i] > intersections[maxIntersectionValue]) {
- maxIntersectionSegments = "";
- maxIntersectionSegments += to_string(i + 1);
- maxIntersectionValue = i;
- }
- }
- if (intersections[maxIntersectionValue] == 0) {
- cout << "Отрезки не пересекаются.";
- }
- else {
- cout << "Наибольшее количество пересечений имеет(ют) отрезок(и) " << maxIntersectionSegments << ". Оно равно " << intersections[maxIntersectionValue] << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement