12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
include <iostream> //#include<fstream> using namespace std; int gcd(int a, int b) { if(b == 0) { return a; } else { return gcd(b, a % b); } } int extended_gcd(int a,int b) { int x; int y; if (a%b==0) { return (0, 1); } else { (x, y) = extended_gcd(b, a%b); return (y, x-(y*(a/b)); } } int main() { int a,b; //ofstream outf("f://output.txt",ios::out); cout << "Input first number: "; //outf<< "Input first number: "; cin >> a; //outf << a<<endl; cout << "Input second number: "; //outf<<"Input second number: "; cin >> b; //outf<< b<<endl; cout << "Greatest common divisior (GCD) is " << gcd(a,b) << endl; //outf<<"Greatest common divisior (GCD) is " << gcd(a,b) << endl; cout<<"The extended Euclid is "<<extended_gcd(a,b)<<endl; system("pause"); return 0; }
#include <iostream> //#include<fstream> using namespace std; int gcd(int a, int b) { if(b == 0) { return a; } else { return gcd(b, a % b); } } int extended_gcd(int a,int b) { int x; int y; if (a%b==0) { return (0, 1); } else { (x, y) = extended_gcd(b, a%b); return (y, x=(y*(a/b))); } } int main() { int a,b; //ofstream outf("f://output.txt",ios::out); cout << "Input first number: "; //outf<< "Input first number: "; cin >> a; //outf << a<<endl; cout << "Input second number: "; //outf<<"Input second number: "; cin >> b; //outf<< b<<endl; cout << "Greatest common divisior (GCD) is " << gcd(a,b) << endl; //outf<<"Greatest common divisior (GCD) is " << gcd(a,b) << endl; cout<<"The extended Euclid is "<<extended_gcd(a,b)<<endl; system("pause"); return 0; }
(x, y) = extended_gcd(b, a%b);
return (y, x=(y*(a/b)));
int
std::pair<int>
123456
int gcd(int a, int b){ if(b == 0) return a; else return gcd(b, a % b); }
Desk test: gcd(12, 15); //a<b call gcd(15, 12%15) -> gcd(15, 12) //now a>b call gcd(12, 15%12) -> gcd(12, 3) call gcd(3, 12 % 3) -> gcd(3, 0) return 3;
if a<b then a%b = a