Advertisement
dmkozyrev

diofant2

Nov 18th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. void diff(vector < int >&a, vector < int >b, int k = 1);
  7. void diofant(int a, int b, int c, int &x, int &y);
  8.  
  9. int main()
  10. {
  11.     int a = -11, b = -58, c = 100, x, y;
  12.     diofant(abs(a), abs(b), c, x, y);
  13.     if (a < 0)
  14.         x = -x;
  15.     if (b < 0)
  16.         y = -y;
  17.     cout << " x = " << x << "+ (" << -b << ")t" << endl;
  18.     cout << " y = " << y << "+ (" << a << ")t" << endl;
  19.     auto flag = a * x + b * y == c ? "true" : "false";
  20.     cout << "Checking : " << flag;
  21.     return 0;
  22. }
  23.  
  24. void diff(vector < int >&a, vector < int >b, int k)
  25. {
  26.     for (int i = 0; i < a.size(); a[i++] -= k * b[i]);
  27. }
  28.  
  29. void diofant(int a, int b, int c, int &x, int &y)
  30. {
  31.     vector < int >v = { a, c, 0 }, w =
  32.     {
  33.     b, 0, c};
  34.     while (v[0] != 0 && w[0] != 0)
  35.     {
  36.         if (v[0] >= w[0])
  37.             diff(v, w);
  38.         else
  39.             diff(w, v);
  40.     }
  41.  
  42.     if (v[0] == 0)
  43.     {
  44.         x = w[1];
  45.         y = w[2];
  46.     }
  47.     else
  48.     {
  49.         x = v[1];
  50.         y = v[2];
  51.     }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement