最大公约数——辗转相除法

int fun (int x,int y)
 {
    
    
 	int i,temp;
 	if(x<y)
 	{
    
    
 		temp=x;
 		x=y;
 		y=temp;  //保证在里面的x>y
	}
 	i=x%y;
 	while(i)
 	{
    
    
 		x=y;
 		y=i;
 		i=x/y;
	}
 	return y;    // 此时返回值为x,y最大公约数 
 }

在这里插入图片描述

辗转相除法
x=6 y=4,所得 2 为最大公约数

猜你喜欢

转载自blog.csdn.net/qq_51333166/article/details/113901753
今日推荐