欧几里得
#include<iostream>
using namespace std;
int gcd(int a,int b){
if(a%b==0)
return b;
else
gcd(b,a%b);
}
int main(){
int a,b;
cin>>a>>b;
cout<<gcd(a,b);
return 0;
}
拓展欧几里得
#include<iostream>
using namespace std;
int exGcd(int a ,int b,int &x,int &y){
// int x,y;
if(b==0){
x=1;y=0;
return a;
}
int r=exGcd(b,a%b,x,y);
int t=x;
x=y;
y=t-a/b*y;
cout<<r<<"="<<x<<"*"<<a<<"+"<<"("<<y<<"*"<<b<<")"<<endl;
return x;
}
int main(){
int a,b,x,y;
cin>>a>>b;
cout<<exGcd(a,b,x,y)<<endl;
return 0;
}