今天带大家用三种方法找到最大公因数。
法一:普法
int main()
{
int up, down,u,d,r;
scanf("%d/%d", &up, &down);
u = up>down?up:down;
d = down+up-u;
if(u%d==0)
{
up =up/ d;
down =down/ d;
}
else
{
r=d/2;
while (u%r!=0||d%r!=0)
r--;
up =up/ r;
down =down/ r;
}
printf("%d/%d", up, down);
return 0;
}
法二:辗转相除法
int main()
{
int up, down,u,d,r;
scanf("%d/%d", &up, &down);
u = up;
d = down;
while (d)
{
r = u % d;
u = d;
d = r;
}
up =up/ u;
down =down/ u;
printf("%d/%d", up, down);
return 0;
}
法三:辗转相减
int main()
{
int up,down,u,d;
scanf("%d/%d",&u,&d);
down=d,up=u;
while(u!=d)
{
while(u>d)
u-=d;
while(u<d)
d-=u;
}
down/=d;
up/=u;
printf("%d/%d", up, down);
return 0;
}
今天就到这里了,不要忘了点赞收藏关注!!!
标签:最简,int,scanf,up,down,while,约分,main,分式 From: https://blog.csdn.net/Giants2024/article/details/144064812