您的位置:首页 > 其它

给定2个数,求出它们的最大公约素数

2014-06-16 00:42 197 查看
华为OJ上的题目,我的答案只有98分,可能还有1,2组数据没过。。。一下子不知道为什么,以后再研究,该睡觉了

j
#include <cstdio>
#include <iostream>
using namespace std;
int gcd(int a,int b)
{
return a%b==0?b:gcd(b,a%b);
}
int main()
{
int n,m,i,ans;
cin>>n>>m;
int t,tmp=gcd(n,m);
if(tmp==1)ans=0;
t=tmp;
while(t>1)
{
for(i=2;i<=t;i++)
{
while(t%i==0)
{
t=t/i;
ans=i;
}
}
}
cout<<ans<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐