Ich habe ein Problem mit dem erweiterten Algorithmus von Euclid. (ax + by = gcd (a, b)) Ich versuche sowohl die GCD als auch x und y zu bestimmen. Die GCD ist kein Problem, aber mit der Loop-Methode läuft etwas falsch mit x und y. Normalerweise erscheint eine Zahl als 0 und die andere als ungewöhnlich große negative Zahl. Code folgt:erweiterter euklid-Algorithmus C++
#include <iostream>
using namespace std;
main()
{
int a,b,q,x,lastx,y,lasty,temp,temp1,temp2,temp3;
cout << "Please input a" << endl;
cin >> a;
cout << "Please input b" << endl;
cin >> b;
if (b>a) {//we switch them
temp=a; a=b; b=temp;
}
//begin function
x=0;
y=1;
lastx=1;
lasty=0;
while (b!=0) {
q= a/b;
temp1= a%b;
a=b;
b=temp1;
temp2=x-q*x;
x=lastx-q*x;
lastx=temp2;
temp3=y-q*y;
y=lasty-q*y;
lasty=temp3;
}
cout << "gcd" << a << endl;
cout << "x=" << lastx << endl;
cout << "y=" << lasty << endl;
return 0;
}
Ihnen so vielen Dank! – user1735851