'commit'
This commit is contained in:
35
C++专题课程/基础知识/求最大公约数(GCD)和最小公倍数(LCM).cpp
Normal file
35
C++专题课程/基础知识/求最大公约数(GCD)和最小公倍数(LCM).cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user