Created
August 2, 2017 08:21
-
-
Save vigack/dcc36a8140f0a7af03629dc528863771 to your computer and use it in GitHub Desktop.
最大公因数、最小公倍数相关
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int gcd(int a, int b){ | |
int rmd; // remainder | |
if(a<b) // insure a>b | |
return gcd(b,a); | |
while((rmd=a%b)!=0){ | |
a = b; | |
b = rmd; | |
} // loop down, b is the gcd | |
return b; | |
} | |
int lcm(int a, int b){ | |
return a*b/gcd(a,b); | |
} | |
int multiGcd(int size, int *arr){ | |
int mg = arr[0]; | |
for(int i=1;i<size;i++) | |
mg = gcd(mg, arr[i]); | |
return mg; | |
} | |
int multiLcm(int size, int *arr){ | |
int ml = arr[0]; | |
for(int i=1;i<size;i++) | |
ml = lcm(ml, arr[i]); | |
return ml; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment