Skip to content

Instantly share code, notes, and snippets.

@chomado
Created August 4, 2014 04:13
Show Gist options
  • Save chomado/954028ff2eb2ec1ec025 to your computer and use it in GitHub Desktop.
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
#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";
}
}
@chomado
Copy link
Author

chomado commented Aug 4, 2014

include

して
__gcd(a,b)
でもいけるらしい

@chomado
Copy link
Author

chomado commented Aug 4, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment