辗转相余求最大公约数

来源:互联网 发布:英译汉软件免费下载 编辑:程序博客网 时间:2024/06/07 22:49

比如15 12

15%12==3

12%3==0

那么最大公约数就是3

int gcd(int a,int b){    if(b>a)    {        int temp=a;a=b;b=temp;    }    while(a%b!=0)    {        a=a%b;        if(b>a)        {            int temp=a;a=b;b=temp;        }    }    return b;}