Advertisement
StoneHaos

4

Jan 23rd, 2022
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. const int n = 4;
  4. const int m = 3;
  5. int a[n][m] = {
  6.     {7, 5, -40},
  7.     {-5, 4, -6},
  8.     {1, 2, -8},
  9.     {0, 1, -4}
  10. };
  11. //Знаки =, >=, <= в уравнениях (1 -> >=) (0 -> =) (-1 -> <=)
  12. int b[n] = {-1, -1, 1, -1};
  13.  
  14. double max(double a, double b) {
  15.     return ((a > b) ? a : b);
  16. }
  17.  
  18. double L(double x, double y) {
  19.     return x + 2 * y;
  20. }
  21.  
  22. bool intersection(int *f1, int *f2, double *x, double *y) {
  23.     double a = f1[0] * f2[1] - f2[0] * f1[1];
  24.     if (a == 0)
  25.         return false;
  26.     *x = (f2[2] * f1[1] - f1[2] * f2[1]) / a;
  27.     *y = (f2[0] * f1[2] - f1[0] * f2[2]) / a;
  28.     return true;
  29. }
  30.  
  31. bool check_condition(double x, double y) {
  32.     bool result = true;
  33.     for (int i = 0; i < n; ++ i) {
  34.         double v = a[i][0] * x + a[i][1] * y + a[i][2];
  35.         if (b[i] == 0)
  36.             result &= (v == 0);
  37.         else
  38.             result &= (v * b[i] >= 0);
  39.     }
  40.     return result;
  41. }
  42.  
  43. int main(void) {
  44.     bool iswrite = false;
  45.     double mx, xmx, ymx;
  46.     for (int i = 0; i < n; ++ i) {
  47.         for (int j = 0; j < n; ++ j) {
  48.             if (i == j) continue;
  49.             double x, y;
  50.             if (intersection(a[i], a[j], &x, &y) && check_condition(x, y)) {
  51.                 //printf("! %f %d %d\n", L(x, y), i, j);
  52.                 if (!iswrite) {
  53.                     mx = L(x, y);
  54.                     xmx = x;
  55.                     ymx = y;
  56.                     iswrite = true;
  57.                 }
  58.                 else {
  59.                     double f = L(x, y);
  60.                     if (f > mx) {
  61.                         mx = f;
  62.                         xmx = x;
  63.                         ymx = y;
  64.                     }
  65.  
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     printf("%f -> (%f, %f)\n", mx, xmx, ymx);
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement