辗转相除法——-欧几里得算法求最大公因数——-求gcd #include<cstdio> using namespace std; int Eu(int a,int b) { if(!b)return a; else return Eu(b,a%b); } int main() { int n,m; scanf("%d%d",&n,&m); printf("%d ",Eu(n,m)); return 0; }
而最小公倍数则是两数之积除gcd();
#include<cstdio> #include<iostream> using namespace std; int gcd(int a,int b) { if(!b)return a; return gcd(b,a%b); } int main() { int n,m; scanf("%d%d",&n,&m); if(n>m)swap(n,m); printf("%d %d ",gcd(n,m),n*m/gcd(n,m)); return 0; }