Advertisement
dmkozyrev

diophant

Nov 18th, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. bool diophant(int a, int b, int c, int& x, int& y){
  5.     stack<int> q; int r;
  6.     while ( a % b != 0 ){
  7.         r = a % b;
  8.         q.push(a/b);
  9.         a = b;
  10.         b = r;
  11.     }
  12.     if ( b != 1 ) {
  13.         return false;
  14.     } else {
  15.         int temp;
  16.         x = 1; y = q.top(); q.pop();
  17.         while ( !q.empty() ){
  18.             temp = x;
  19.             x = - y;
  20.             y = - temp - q.top()*y;
  21.             q.pop();   
  22.         }
  23.         x *= c; y *= -c;
  24.         return true;
  25.     }
  26. }
  27.  
  28. int main(){
  29.     int a, b, c, x, y;
  30.     cout << "Input a, b and c (ax + by = c): ";
  31.     cin >> a >> b >> c;
  32.     if ( diophant(a,b,c,x,y) ){
  33.         cout << "x = " << x << " - " << b << "*t" << endl;
  34.         cout << "y = " << y << " + " << a << "*t";
  35.     } else {
  36.         cout << "It isn't Diophantine equation!";
  37.     }  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement