三种方法:
求此数有三种思想,一是定义法求解,二是相减求等法,三是辗转相除法。具体算法如下。
定义法
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int x = n<m?n:m;
int i;
for(i=x;i>=1;i--)
{
if(n%i==0&&m%i==0)break;
}
cout<<i<<endl;
return 0;
}
相减求等法: 更相减损术
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
while(n!=m)
{
if(n>m)n=n-m;
else m=m-n;
}
cout<<m<<endl;
return 0;
}
辗转相除法:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int a;
while((a=n%m)!=0)
{
n=m;
m=a;
}
cout<<m<<endl;
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/92921.html