首页 > 编程语言 > 详细

扩展欧几里得算法

时间:2016-08-11 13:05:05      阅读:161      评论:0      收藏:0      [点我收藏+]

扩展欧几里得算法,可以在计算gcd(a,b)的同时,计算出

ax+by=gcd(a,b)中a、b的值

#include<stdio.h>
#include<iostream>
using namespace std;
 int ext_gcd(int a, int b, int *x, int *y)
 {
     if(b==0)
     {
         *x = 1,*y = 0;
         return a;
     }
     else
     {
         int r = ext_gcd(b, a%b, x, y);
         int t = *x;
         *x = *y;
         *y = t - a/b * *y;
         return r;
     }
 }
int main()
{
    int a,b;
    int x,y;
    while(1)
    {
        cin>>a>>b;
        cout<<ext_gcd(a,b,&x,&y)<<endl;
        cout<<x<<"  "<<y<<endl;
    }
}

 

扩展欧几里得算法

原文:http://www.cnblogs.com/superxuezhazha/p/5760395.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!