使用辗转相除法来实现求两个数的最大公约数。
只需要先输入两个数字,然后就能输出这两个数字的最大公约数。
#include <iostream>
#include <algorithm>
using namespace std;
int gcd(int a,int b){
if(b == 0) return a;
else return gcd(b,a % b);
}
int main(){
int m,n;
cin >> m >> n;
cout << gcd(max(m,n),min(m,n));
return 0;
}
/*
sample input:
12 8
sample output:
4
*/
本文介绍了一种使用辗转相除法实现求两个数最大公约数的算法。通过递归的方式,输入任意两个整数,即可快速准确地得到它们的最大公约数。
1032

被折叠的 条评论
为什么被折叠?



