首页 > 其他 > 详细

最大公约数和最小公倍数问题

时间:2019-07-25 13:08:38      阅读:83      评论:0      收藏:0      [点我收藏+]

题目见这里:https://www.luogu.org/problem/P1029

codes 1:

#include<bits/stdc++.h>
using namespace std;
int m,n,ans=0;
int main()
{
	cin>>n>>m;
	for(int i=1;i<=sqrt(m*n);i++)
	{
		if((n*m)%i==0 && __gcd(i,(n*m)/i)==n) ans++;
	}
	cout<<ans*2<<endl;
	return 0;
}

codes 1之优化:

#include<bits/stdc++.h>
using namespace std;
int m,n,ans=0;
int main()
{
	cin>>n>>m;
	for(int i=n;i<=sqrt(m*n);i++)//对称性!
	{
		if((n*m)%i==0 && __gcd(i,(n*m)/i)==n) ans++;
	}
	cout<<ans*2<<endl;
	return 0;
}

codes1——gcd

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int gcd(int x,int y){return !y?x:gcd(y,x%y);}  //精辟简洁的函数语句
 4 int main()
 5 {
 6     int x,y;
 7     scanf("%d%d",&x,&y);
 8     printf("%d\n",gcd(x,y));
 9     printf("%d\n",__gcd(x,y));
10     return 0;
11 }

 

最大公约数和最小公倍数问题

原文:https://www.cnblogs.com/dragondragon/p/11243466.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!