1.gcd算法的迭代版本
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
int tmp;
while(b!=0)
{
tmp=b;
b=a%b;
a=tmp;
}
if(b==0)
return a;
}
int main()
{
int a,b;
cin>>a>>b;
cout<<gcd(a,b);
return 0;
}
2.EGCD算法
#include <iostream>
using namespace std;
void egcd(int a,int b)
{
int r0=1;
int r1=0;
int s0=0;
int s1=1;
while(b)
{
int q=a/b;
a=b;
b=a%b;
r0==r1;
r1=r0-q*r1;
s0=s1;
s1=s0-q*s1;
}
cout<<a<<" "<<r0<<" "<<s0;
}
int main()
{
int a,b;
cin>>a>>b;
egcd(a,b);
return 0;
}