Last active
December 12, 2015 03:38
-
-
Save mingtsay/4708640 to your computer and use it in GitHub Desktop.
GCD & LCM Reference
This file contains hidden or 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
| #include <stdio.h> | |
| int gcd(int a, int b) | |
| { | |
| int m; | |
| while(b) | |
| { | |
| m = b; | |
| b = a % b; | |
| a = m; | |
| } | |
| return a; | |
| } | |
| int lcm(int a, int b) | |
| { | |
| return a * b / gcd(a, b); | |
| } | |
| int main() | |
| { | |
| int n, i, l; | |
| while(scanf("%d", &n) != EOF && n > 0) // 新測資 | |
| { | |
| scanf("%d", &i); // 第一筆資料 | |
| l = i; | |
| for(--n; n; --n) // 剩下的資料 | |
| { | |
| scanf("%d", &i); | |
| l = lcm(l, i); | |
| } | |
| printf("%d\n", l); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment