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

请问如何用C++实现欧几里德算法

SwanK 发布于 2013-02-13 17:59, 1014 次点击
我做磨了一个晚上也没有出来!请教!谢谢!
欧几里德算法


[ 本帖最后由 SwanK 于 2013-2-13 18:16 编辑 ]
34 回复
#2
Susake2013-02-13 18:02
瞧一瞧,看一看
#3
zklhp2013-02-13 18:11
欧几里得算法是啥啊 能用语言描述描述么

实现不了 两种可能

1 语言不会 不知道怎么把算法用语言实现
2 算法不会 不知道用什么算法或模型

祝楼主好运~
#4
Susake2013-02-13 18:19
没听说过
#5
SwanK2013-02-13 18:37
题目如下:
找出两个任意正整数的最大公约数,例如 x 和 y ,它们的公约数例如是z. 用loop的方式来写程序。
让用户自己任意输入2个数字-任意正整数,然后经过loop 的方式来实现输出 最大公约数z.
谢谢!
#6
SwanK2013-02-13 18:39
1 语言不会 不知道怎么把算法用语言实现,楼上的专家说对了。我着急啊!
#7
Susake2013-02-13 18:48
#include "stdio.h"
#include "conio.h"
main()
{
  int a,b,num1,num2,temp;
  printf("please input two numbers:\n");
  scanf("%d,%d",&num1,&num2);
  if(num1<num2)/*交换两个数,使大数放在num1上*/
  {
    temp=num1;
    num1=num2;
    num2=temp;
  }
  a=num1;b=num2;
  while(b!=0)/*利用辗除法,直到b为0为止*/
  {
    temp=a%b;
    a=b;
    b=temp;
  }
  printf("gongyueshu:%d\n",a);
  getch();
}
#8
SwanK2013-02-13 18:51
你好快啊!用c++啊。
#include "stdio.h"
 #include "conio.h"---这是什么?

我看课本一般是
#include<iostream>
using namespace std;
....
#9
Susake2013-02-13 18:56
改下就是了
#include <iostream>
using namespace std;
#include "conio.h"
main()
{
  int a,b,num1,num2,temp;
  cout << "please input two numbers" << endl;
  cin >> num1 >> num2;
  if(num1<num2)/*交换两个数,使大数放在num1上*/
  {
    temp=num1;
    num1=num2;
    num2=temp;
  }
  a=num1;b=num2;
  while(b!=0)/*利用辗除法,直到b为0为止*/
  {
    temp=a%b;
    a=b;
    b=temp;
  }
  cout << "gongyueshu:" << a << endl;
  getch();
}
#10
SwanK2013-02-13 19:03
看看这个:你是不是用C而不是C++.我没有学过C

 #include <iostream>

using namespace std;

 int main()
 {
   int a,b,num1,num2,temp;

   cout <<"please input two numbers:\n" <<endl;

   cin >> num1>> num2;

   if(num1<num2)         //Exchange two numbers, so that large numbers on num1

   {
     temp=num1;
     num1=num2;
     num2=temp;
   }

   a=num1;b=num2;

   while(b!=0)           //Rolling division until b is 0
   {
     temp= a % b;
     a=b;
     b=temp;
   }
   cout<< "The gcd of number is:"<<a;

   return 0;
 }

#11
SwanK2013-02-13 19:05
以上对不对了?帮我运行一下,看有无BUG?谢谢了!
还有一题,我另外开出。请帮忙!
#12
Susake2013-02-13 19:09
把最后的a换成num1 * num2 / a;
#13
Susake2013-02-13 19:10
我可以
#14
SwanK2013-02-13 19:23
为什么要把最后的a换成num1 * num2 / a; ?
#15
Susake2013-02-13 19:27
a的话,算出来是最小公约数
而这个是最大公倍数
#16
SwanK2013-02-13 19:28
我明白了。
但是我运行的时候 有一处错,但是我不知道那里错。
我不能输入2变量
#17
Susake2013-02-13 19:28
你可以用刚刚那个算出a,再用loop算出num1 * num2 / a应该就满足题意了
#18
Susake2013-02-13 19:29
用空格做分隔,我的可以啊
#19
SwanK2013-02-13 19:30
要不要对变量先初始化?
例如
num1=0;
num2=0;
a=0;
...
#20
Susake2013-02-13 19:32
不要,num1 和num2 是你自己输入的
#21
SwanK2013-02-13 19:43
看,错在那?这 行字我看不到 我用dos的"please input two numbers:\n";

#include <iostream>


using namespace std;

int main()
{
    int a,b,num1,num2,temp;

    cout << "please input two numbers:\n";

    cin >> num1 >> num2;

    cout << endl;

    if (num1 < num2)         //Exchange two numbers, so that large numbers on num1

    {
        temp = num1;
        num1 = num2;
        num2 = temp;
    }

    a = num1;
    b = num2;

    while(b!=0)           //Rolling division until b is 0
    {
        temp = a % b;
        a = b;
        b = temp;
    }
    cout << "The gcd of number is:" << num1 * num2 / a;

    return 0;
}
#22
SwanK2013-02-13 19:44
"The gcd of number is:" -这也看不到
#23
Susake2013-02-13 19:52
我运行是对的输入4和6输出2和12
#24
SwanK2013-02-13 20:01
我输入4 6,得出2,但看不到12
为什么有12
#25
SwanK2013-02-13 20:05
对了,可否用 n=n+1的方法做计数变量? n++

#26
Susake2013-02-13 20:07
可以啊
#27
SwanK2013-02-13 20:10
请帮忙啊,因为我们正学这个,一定能够是需要放入的。谢谢!
#28
Susake2013-02-13 20:11
说清楚点,什么正学,放哪里?
#29
SwanK2013-02-13 20:25
在程序中加入计数器,在loop中让程序自动进行除法.
例如:
do while
n=0
n=n+1
num1%num2
。。。


#30
Susake2013-02-13 20:30
程序代码:
while(n < 10)
{
   k = num1 % num2;
   n++;
}
#31
SwanK2013-02-13 20:31
突然发现这个
/*
EUCLIDEAN
*/
#include <iostream>
using namespace std;
unsigned int Gcd(unsigned int a, unsigned int b)
{
unsigned int temp;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
#32
Susake2013-02-13 20:32
......
#33
SwanK2013-02-13 20:36
为什么n<10? 好像题意没有限制
#34
SwanK2013-02-14 14:20
谢谢 这位高手的耐心帮助。很聪明!再次感谢!
我很喜欢这个论坛,可以互相学到很多东西,大家都很友好!
#35
Susake2013-02-14 14:21
。。。。
1