ALDS1_1_B | Greatest Common Divisor
Table of contents
# Problem
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_1_B
Given 2 numbers, report the GCD of the 2 numbers.
# Explanation
# Time complexity
※ is the count of modulo arithmetics
# Solution
Int X, Y;
Int gcd(Int x, Int y) {
if (x > y) swap(x, y);
Int r;
while (y > 0) {
r = x % y;
x = y;
y = r;
}
return x;
}
void input() {
cin >> X >> Y;
}
void solve() {
cout << gcd(X, Y) << endl;
}
int main() {
input();
solve();
return 0;
}
Shun
Remote freelancer. A web and mobile application enginner.
Traveling around the world based on East Asia.
I'm looking forward to your job offers from all over the world!