求最大公约数与最小公倍数实例C++程序代码

来源:互联网 发布:php js跨域解决方案 编辑:程序博客网 时间:2024/05/05 18:29

1、求二个数的最大公约数:

#include <iostream.h>
int maxye(int a,int b)
{
int temp;
while(a%b)
{
  temp=b;
  b=a%b;
  a=temp;
}
return b;
}

void main()
{
int aa,bb;
cout<<"请输入第一个数:";
cin>>aa;
cout<<" 请输入第二个数:";
cin>>bb;
cout<<"这二个数的最大公约数是:"<<maxye(aa,bb)<<endl;
}

 

2、求二个数的最小公倍数

#include <iostream.h>
int maxye(int a,int b)
{
int temp;
if(a<b)
{
  temp=a;
  a=b;
  b=temp;
}
for(int i=1;i<=b;i++)
{
if(!((a*i)%b))
{
  return a*i;
}
}

}

void main()
{
int aa,bb;
cout<<"请输入第一个数:";
cin>>aa;
cout<<" 请输入第二个数:";
cin>>bb;
cout<<"这二个数的最小公倍数是:"<<maxye(aa,bb)<<endl;
}

原创粉丝点击