C++ 实现求最大公约数和最小公倍数,最大公约数
辗转相除法:
int maxDivisor(int a, int b){ int c = b; while (a%b != 0){ c = a%b; a = b; b = c; } return c; }
辗转相减法:
int maxDivisor(int a, int b){ while (a != b){ if (a>b) a = a - b; else b = b - a; } return a; }
2024-04-12 18:29:29
C++ 实现求最大公约数和最小公倍数,最大公约数
辗转相除法:
int maxDivisor(int a, int b){ int c = b; while (a%b != 0){ c = a%b; a = b; b = c; } return c; }
辗转相减法:
int maxDivisor(int a, int b){ while (a != b){ if (a>b) a = a - b; else b = b - a; } return a; }