The common denominator of the calculation method

/ / Find the greatest common divisor
// The first method: enumeration method
/ / start every number from 1 to check whether the number of conventions,
// and constantly updated to the maximum number (only need to compare the two numbers lower number)
#include <stdio.h>
int main ()
{
int a, B;
Scanf ( "% D% D", a &, & B);
int min, RET;
IF (a <B)
min = a ;
the else B min =;
for (int I =. 1; I <min; I ++)
{
IF (a% I == 0) {
IF (I% B == 0)
{
RET = I ; // value conforming to assigned to ret, with each update will get the latest
}
}
}
the printf ( "% d% d is the greatest common divisor and% d", a, B, RET);


// second method: Euclidean division
// a% b = ta = tb = t until t == 0

Way of thinking: the remainder is b can not be offset by a multiple of the number of divided again to find the gap between them, so that the division can continue to find the smallest of the

#include <stdio.h>
int main ()
{
int a,b;
scanf("%d %d",&a,&b);
int t;
while (t!=0)
{
t=a%b;
a=b;
b=t;
}
printf("%d",a);
return 0;

}

Guess you like

Origin www.cnblogs.com/wengst/p/11819224.html