Enter two positive integers, the output of which is the greatest common divisor.

Euclidean

Divisor and remainder division repeatedly done, when the remainder is 0, the equation takes the current divisor is a greatest common divisor.

# include<stdio.h>
int main()
{
	int num1,num2,t=1;
	scanf("%d %d",&num1,&num2);
	while(t)         //辗转相除法
	{
		t=num1%num2;
		num1=num2;
		num2=t;
	 } 
	 printf("%d",num1);
	 return 0;
}

For the greatest common divisor may also be used: Decreases cytometry. But to consider the size of its num1 and num2.

Decreases Technique

First step: two for any given positive integer; determining whether they are even. If so, then with 2 reduction; if not then the second step.
The second step: reducing the number of larger number of smaller, then comparing the resulting difference is small and the number, and to reduce the number of large numbers. This operation continues until the subtrahend and difference resultant equal.
Average number of product and a second step the product is the first step out of the plurality of about 2 is required the greatest common divisor.
Wherein said, "and several" is the divisor. Seek "and several" approach is "Decreases" method.

# include<stdio.h>
# include<math.h>
int main()
{
	int num1,num2,count=0;
	scanf("%d %d",&num1,&num2);
    if((num1%2==0)&&(num2%2==0))
    {
    	num1/=2;
    	num2/=2;
    	count++;
	}     //减少运算量
	while(num1!=num2)  //更相减损术
	{
		if(num1>num2)
		{
			num1-=num2;
		}
		else
		{
			num2-=num1;
		}
	}
	num1=num1*pow(2,count);
	 printf("%d",num1);
	 return 0;
}
Published 43 original articles · won praise 1 · views 819

Guess you like

Origin blog.csdn.net/Du798566/article/details/104192603