Advertisement
StoneHaos

m_mod_4~

Mar 29th, 2021
1,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. void intersection(int* f1, int* f2, double& x, double& y) {
  9.     double a = f1[0] * f2[1] - f2[0] * f1[1];
  10.     x = (f2[2] * f1[1] - f1[2] * f2[1]) / a;
  11.     y = (f2[0] * f1[2] - f1[0] * f2[2]) / a;
  12.  
  13. }
  14.  
  15. int main(void)
  16. {
  17.     setlocale(LC_ALL, "Russian");
  18.     int cel[3];
  19.     int n;
  20.     int mode;
  21.  
  22.     cout << "Введите номер варианта:" << endl << "1. Максимизировать (По умолчанию)" << endl << "2. Минимизировать" << endl;
  23.     cin >> mode;
  24.     if (mode != 1 && mode != 2) mode = 1;
  25.  
  26.     cout << "Введите количество уравнений:" << endl;
  27.     cin >> n;
  28.     int** arr = new int* [n];
  29.     int* values = new int[n];
  30.     for (int i = 0; i < n; ++i) arr[i] = new int[3];
  31.  
  32.     cout << "Уравнения должны иметь вид: ax1 + bx2 + c = 0" << endl;
  33.     cout << "Введите целевую функцию по коэффициентам:" << endl;
  34.     cout << "a:" << endl;
  35.     cin >> cel[0];
  36.     cout << "b:" << endl;
  37.     cin >> cel[1];
  38.     cout << "c:" << endl;
  39.     cin >> cel[2];
  40.  
  41.     cout << "Вводите уравнения в системе по коэффициентам:" << endl;
  42.     for (int i = 0; i < n; ++i) {
  43.         cout << "Уравнение " << i + 1 << ":" << endl;
  44.         cout << "a:" << endl;
  45.         cin >> arr[i][0];
  46.         cout << "b:" << endl;
  47.         cin >> arr[i][1];
  48.         cout << "c:" << endl;
  49.         cin >> arr[i][2];
  50.         cout << endl;
  51.     }
  52.  
  53.     for (int i = 0; i < n; ++i) {
  54.         double x, y;
  55.         intersection(arr[i], arr[(i + 1) % n], x, y);
  56.         cout << x << " " << y << endl;
  57.         values[i] = x * cel[0] + y * cel[1] + cel[2];
  58.     }
  59.     if (mode == 1) {
  60.         cout << "Максимальное значение: " << *max_element(values, values + n) << endl;
  61.     }
  62.     else {
  63.         cout << "Минимальное значение: " << *min_element(values, values + n) << endl;
  64.     }
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement