注册 登录
编程论坛 C++教室

我编的求两个数的最大公约数和程序有问题,请大家帮忙看一下.

lub1990 发布于 2009-09-14 13:18, 568 次点击
源码如下:
程序代码:

#include<iostream>
void yuns();
void yu();
int main()
{
    using namespace std;
   
    int a,b,e,f,t;
    float c,d;
   
    cout<<"请输入两个数:";
    cin>>a>>b;              
   
    if(a>=b)
            a=a,b=b;
    else
        t=a;
        a=b;
        b=t;
    e=0;   
     
     yuns();
  
     system("pause");
     return 0;
}            
                          

void yuns()
{
     int a,b,e,f,t;
    float c,d;
     f=b-e;
     c=a/f;
     d=b-f;
     yu();
}
void yu()
{
     using namespace std;
     int a,b,e,f,t;
    float c,d;
     if(c-(int)c==0)
     {
         if(d-(int)d==0)
             cout<<"\n最大公约数为:"<<f;
          else
              e++;
              yuns();
     }
     else
              e++;
              yuns();
}




结果是:
只有本站会员才能查看附件,请 登录



[ 本帖最后由 lub1990 于 2009-9-14 13:47 编辑 ]
5 回复
#2
forclwy2009-09-14 18:29
感觉程序好乱
#3
choco10242009-09-14 22:57
#include <iostream>
using namespace std;
int main()
{
    int p,r,n,m,temp;
    cout<<"please input the integer numbers n,m:";
    cin>>n>>m;
    if(n<m)
    {
        temp=n;n=m;m=temp;  //把大数放在n中,小数放在m中
    }
    p=n*m;
    while(m!=0)           //求m,n的最大公构数
    {
        r=n%m;
        n=m;
        m=r;
    }
    cout<<"最大公约数为:"<<n<<endl;
    cout<<"最小公倍数为:"<<p/n<<endl;
    return 0;
}
 
#4
lub19902009-09-14 23:15
回复 3楼 choco1024
谢谢你,这个程序可用,但是有一点我不懂,其中
while(m!=0)           //求m,n的最大公构数
    {
        r=n%m;
        n=m;
        m=r;
    }
这是什么原理?
#5
lintaoyn2009-09-15 10:51
回复 4楼 lub1990
这个可以去问欧几里德~~~~
#6
lub19902009-09-15 12:02
回复 5楼 lintaoyn
谢了.
1