1.更相减损术
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int a,b,t;
cin >> a>> b;
while(a!=b)
{
t=abs(a-b);
a=b;
b=t;
}
cout << a << endl;
return 0;
}
2.辗转相除法
#include <iostream>
using namespace std;
#include <cmath>
int main ()
{
int a,b;
cin >>a >> b;
while(a%b!=0)
{
int t=a%b;
a=b;
b=t;
}
cout << b << endl;
return 0;
}
本文介绍了两种古老的数学算法——更相减损术和辗转相除法(欧几里得算法),分别用于求解两个正整数的最大公约数。通过C++代码实现,详细解释了算法原理及其应用。这两者是数论基础,对于理解计算机科学中的算法和数学概念至关重要。
2176

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



