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

2 years ago
#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;
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC>:շת<D5B7><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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);
//<2F><>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
printf("%d and %d LCM: %d\n", m, n, m*n/gcd);
return 0;
}