欧几里得算法(C++)

#include<iostream>
using  namespace std;

int Eculid(int d,int f)
{
 int X,Y,R;
 X=d;
 Y=f;
 while(Y!=0)
 {
       R=X%Y;
    X=Y;
    Y=R;
 }
 return X;
}

void ExEculid(int d,int f)
{
 int x1,x2,x3;
 int y1,y2,y3;
 int t1,t2,t3;
 int q;
 x1=1;
 x2=0;
 x3=f;
 y1=0;
 y2=1;
 y3=d;
 while(true)
 {
  if(y3==0)
  {
   cout<<"the gcd is(ExEculid):"<<x3<<endl;
            cout<<"No contrary!"<<endl;
   break;
  }
      if(y3==1)
  {
   cout<<"the gcd is(ExEculid):"<<y3<<endl;
   cout<<"the contrary is(ExEculid):"<<y2<<endl;
   break;
  }
     q=x3/y3;
  t1=x1-q*y1;
        t2=x2-q*y2;
  t3=x3-q*y3;
  x1=y1;
  x2=y2;
  x3=y3;
  y1=t1;
  y2=t2;
  y3=t3;
 }
 
}

void main()
{
 int a,b;
 cout<<"please input two numbers:";
 cin>>a;
 cin>>b;
 cout<<"the gcd is(Eculid):"<<Eculid(a,b)<<endl;
    ExEculid(a,b);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值