设计两个函数,分别求出两个数的最大公约数和最小公倍数

mathematical 2007-08-15 04:41:52
如题...3Q先
...全文
2974 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
jack_wq 2007-08-23
  • 打赏
  • 举报
回复
高质量C/C++编程中有这个代码,不过有点小错误(我看的是第二版),可以参考。
hunter0912 2007-08-23
  • 打赏
  • 举报
回复
template < unsigned long N, unsigned long n >
struct gcd {
static const unsigned long value = gcd< n, N % n >::value;
};

template < unsigned long N >
struct gcd< N, 0 > { // specialization(模板具体化),用作终止递归
static const unsigned long value = N;
};

template < unsigned long N, unsigned long n >
const unsigned long gcd< N, n >::value;

#include <iostream>

int main()
{
std::cout << gcd< 1575, 235 >::value << "\nPress ENTER to quit...";

std::cin.get();
return 0;
}
lfcai 2007-08-22
  • 打赏
  • 举报
回复
int max(int a,int b)//最大公约数
{
int t,r;

if(a>b)
{t=a;a=b;b=t;}
r=b%a;
while(r!=0)
{b=a;a=r;r=b%a;}
return(a);
}
//最小公倍数=a*b/max(int a,int b)
  • 打赏
  • 举报
回复
漏了个%d
main()
{int a,b,r;
scanf(“%d %d”,&a,&b);
r=a%b;
while(r!=0)
{ a=b;
b=r;
r=a%b;
}
printf(“最大公约数为:%d”,b);
}

  • 打赏
  • 举报
回复
main()
{int a,b,r;
scanf(“%d%d”,&a,&b);
r=a%b;
while(r!=0)
{ a=b;
b=r;
r=a%b;}
printf(“最大公约数为:”,b);
}

expter 2007-08-18
  • 打赏
  • 举报
回复
#include<iostream>

using namespace std;

int gcd(int a,int b) //最大公约数 迭代法
{
int c=a>b? a:b; //返回大的数
int d=a<b? a:b;
while(d!=0)
{
int e=d;
d=c%d;
c=e;
}
return c;
}
int gcd1(int a, int b) //递归法
{
if(b==0)
return a;
else
return gcd1(b,a%b);
}


int main()
{
int a,b;
cin>>a>>b;
cout<<gcd(a,b)<<endl; //最大公约数
cout<<gcd1(a,b)<<endl; //最大公约数

cout<<a*b/gcd1(a,b)<<endl;
return 0;
}
hpxs2 2007-08-18
  • 打赏
  • 举报
回复
learnning
comman_ndsc 2007-08-18
  • 打赏
  • 举报
回复
碾转法求最大公约数:
int a ,b, y,p;

if (a > b) // a存放较大的数,b存放较小的数。
{
a ^= b;
b ^= a;
a ^ = b;
}

p = a * b; // 先将a和b的乘积保存在p中,以便求最小公倍数时用。
while (b != 0) // 撵转法求a 和 b的最大公约数
{
y = a % b;
a = b;
b = y;
}

最大公约数为:a
最小公倍数为:p/a;





millky 2007-08-18
  • 打赏
  • 举报
回复
哦,经常见的题目,不过欧几里德算法正确,效率最高,很成熟的算法我觉得再讨论意义不大了吧。。。。
wms011618 2007-08-18
  • 打赏
  • 举报
回复
我觉得这个方法效率很高,没有除法,并且迭代速度很快。
可以证明该算法是正确的
wms011618 2007-08-18
  • 打赏
  • 举报
回复
我看到一个有趣的方法,大家讨论一下:
int gcd(int a,int b){
while(a!=b)
{
if(a>b) a-=b;
else b-=a;
}
return a;/*or return b*/
}
vsfan 2007-08-18
  • 打赏
  • 举报
回复
恩恩,几位星星已经说的非常清楚了
星羽 2007-08-18
  • 打赏
  • 举报
回复
// 最大公约数
int common_divisor(int x, int y)
{
int _max = max(x, y);
int _min = min(x, y);
int _tem;

while (_tem = _max % _min)
{
_max = _min;
_min = _tem;
}

return _min;
}

// 最小公倍数
int common_multiple(int x, int y)
{
return (x * y) / common_divisor(x, y);
}

void main()
{
cout<<common_divisor(25, 15)<<endl;
cout<<common_multiple(25, 15)<<endl;
}
MPTD_Fire 2007-08-18
  • 打赏
  • 举报
回复
书本上不是有吗
scrutin 2007-08-15
  • 打赏
  • 举报
回复



int min(int m, int n)
{
int a,b;
a=max(m,n);
if (m>n) //最小公倍数=较大的数*(较小的数/最大公约数)
{
b=n;
b/=a;
return m*b;
}
else
{
b=m;
b/=a;
return n*b;
}
}
lizhaohu 2007-08-15
  • 打赏
  • 举报
回复
int max(int a,int b)//最大公约数
{
int t,r;

if(a>b)
{t=a;a=b;b=t;}
r=b%a;
while(r!=0)
{b=a;a=r;r=b%a;}
return(a);
}
我啃 2007-08-15
  • 打赏
  • 举报
回复
这两个都是最大公约数的欧几里德算法
最小公倍数就是两者的积除以最大公约数
我啃 2007-08-15
  • 打赏
  • 举报
回复
非递归版本:
void swap(int & a, int & b){
int c = a;
a = b;
b = c;
}

int gcd(int a,int b){
if(0 == a ){
return b;
}
if( 0 == b){
return a;
}
if(a > b){
swap(a,b);
}
int c;
for(c = a % b ; c > 0 ; c = a % b){
a = b;
b = c;
}
return b;
}

我啃 2007-08-15
  • 打赏
  • 举报
回复
//两个数的最大公约数--欧几里得算法
int gcd(int a, int b)
{
if (a < b)
swap(a, b);

if (b == 0)
return a;
else
return gcd(b, a%b);
}

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧