最大公因数的话,用欧几里得的辗转相除法。。
最小公倍数的话,最直接就是一个从2到这两数乘积的循环,看哪个数同时被这两数整除。
实际上,根据数学原理,两个数的最小公倍数等于两个数的乘积除以两个数的最大公因数。
#include<stdio.h>
int max(int a,int b)
{
int r;
while(b!=0)
{
r=a%b;
a=b;
b=r;
}
return a;
}
int min(int a,int b)
{
return a*b/max(a,b);
}
int main()
{
int t,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&x,&y);
printf("%d %d\n",max(x,y),min(x,y));
}
return 0;
}
https://blog.csdn.net/weixin_62264287/article/details/122829781?spm=1001.2014.3001.5502
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有