Created
August 4, 2014 04:13
-
-
Save chomado/954028ff2eb2ec1ec025 to your computer and use it in GitHub Desktop.
AOJ, 最大公約数と最小公倍数の問題。http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0005&lang=jp
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
#include <iostream> | |
int Gcd(int a, int b) // Greatest Common Divisor | |
{ | |
if (!a || !b) { | |
return 0; | |
} | |
while (a != b) { | |
if (a < b) | |
std::swap(a, b); | |
a -= b; | |
} | |
return a; | |
} | |
int Lcm(int a, int b, int gcd) // Least Common Multiple | |
{ | |
if (!a || !b) { | |
return 0; | |
} | |
return a * (b / gcd); // 桁あふれ防止のために計算順序考慮 | |
} | |
int main() | |
{ | |
int a, b, gcd; | |
while (std::cin >> a >> b) { | |
gcd = Gcd(a, b); | |
std::cout << gcd << " " << Lcm(a, b, gcd) << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
include
して
__gcd(a,b)
でもいけるらしい