You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
python/C++专题课程/基础知识/求最大公约数(GCD)和最小公倍数(LCM).cpp

36 lines
505 B

#include<bits/stdc++.h>
using namespace std;
void max_min(int &m, int &n) {
int tmp;
if(m<n) {
tmp=m;
m=n;
n=tmp;
}
}
//计算最大公约数:辗转相除法
int Cal_GCD(int m, int n) {
int gcd;
max_min(m, n);
gcd=m%n;
while(gcd) {
m=n;
n=gcd;
gcd=m%n;
}
return n;
}
int main() {
int m, n, gcd;
printf("Enter two num a b: ");
scanf("%d %d", &m, &n);
gcd=Cal_GCD(m, n);
printf("%d and %d GCD: %d\n", m, n, gcd);
//最小公倍数
printf("%d and %d LCM: %d\n", m, n, m*n/gcd);
return 0;
}