使用Euclid算法编写两个整数的最大公约数<辗转相除法>
1.如果a除以b能整除,则最大公约数是b
2.否则,最大公约数等于b和a%b的最大公约数
辗转相除法的原理
377除319=1余58,319除58=5余29,58除29余0,结束
srand((unsigned)time(NULL));
a=rand()%8+1;
b=rand()%8+1;
随机数的选取,这里没有判断整数的正负
</pre><pre name="code" class="cpp">#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int gcd(int a,int b) ;
int main(void)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",gcd(a,b));
return 0;
}
int gcd(int a,int b)
{
int temp;
if(a<b)
{
temp=a;
a=b;
b=temp;
}
while(b!=0)
{
temp=a%b;
a=b;
b=temp;
}
return a;
}