
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,i,q;
cin>>a>>b;
while(a!=b)
{
if(a>b)
a=a-b;
else
if(a<b)
b=b-a;
}
cout<<b;
return 0;
}
其实这道题有三种解法
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,i;
cin>>a>>b;
for(i=b;i>0;i--)
{
if(a%i==0&&b%i==0)
break;
}
cout<<i;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c,i;
cin>>a>>b;
c=a%b;
while(c!=0)
{
a=b;
b=c;
c=a%b;
}
cout<<b;
return 0;
}

本文介绍了三种不同的算法来寻找两个整数的最大公约数:通过减法迭代、循环除法和欧几里得算法。展示了C++代码实例,并强调了它们在实际编程中的应用场景和效率比较。
835

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



