[Templates That Are Usually Used]Greatest Common Divisor
2013-05-13 13:07
351 查看
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; int gcd(int a, int b) { int ans; if(a < b) swap(a, b); if(b == 0) ans = a; else ans = gcd(b, a%b); return ans; } int main() { int a, b, c; cin >> a >> b; c = gcd(a, b); cout << c << endl; return 0; }
//优化:
int gcd(int x, int y)
{
return y == 0 ? x : gcd(y, x%y);
}
相关文章推荐
- [Templates That Are Usually Used]Least Common Multipler
- [Templates That Are Usually Used]Header Files Include and Pre-Process(ToBeContinued)
- BestCoder Round #38 1002.Greatest Greatest Common Divisor
- C# language features that are rarely used but can be useful
- 最大公约数(Greatest Common Divisor ) 与 最小公倍数(Lowest Common Multiple )
- Item29 Assume that move operations are not present,not cheap,and not used
- In the event another person is usually process these individuals marketing that are looking for
- Greatest common divisor
- GCD (Greatest Common Divisor)
- C# language features that are rarely used but can be useful
- SGU 499:Greatest Greatest Common Divisor
- The Greatest Common Divisor
- BestCoder Round #38 1002——数学——Greatest Greatest Common Divisor
- Algorithms - 最大公约数(greatest common divisor)-欧几里得(Euclid) 算法 及 代码
- getGreatestCommonDivisor
- The greatest common divisor gcd(最大公约数)
- Greatest Common Divisor (GCD) - Euclidean algorithm
- HDU 5207 Greatest Greatest Common Divisor
- HDU 5207 Greatest Greatest Common Divisor
- Algorithms - 最大公约数(greatest common divisor)-欧几里得(Euclid) 算法 及 代码