【发布时间】:2015-10-19 20:13:32
【问题描述】:
所以我的代码有问题。
我正在使用欧几里得算法编写一个最大公约数,我似乎无法利用循环来保持除法不断重复,直到我得到最大公约数。所以现在,我能够得到剩余的,但基本上不知道如何从那里继续。
任何帮助将不胜感激!
这是我目前所拥有的
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int a;//Holds the first number
int b;//Holds the second number
int temp;//Assign to greatest number
int hold;//Assign to smaller number
float euclid;//soon to be function?
int leftover;
float gcd;
int main ()
{
cout<<"Welcome to Brian Garnadi's Version of GCD!\n"<<endl;
cout<<"Enter the first integer to be calculated: ";
cin>> a;
cout<<"Now enter the second integer: ";
cin>>b;
if (a>b)//Determines bigger number
{temp=a;
hold=b;
}
if (a<b)//Determines smaller number
{
temp=b;
hold=a;
}
leftover= temp%hold;
cout<<"\nThe remainder of the two numbers divided is "<<leftover<<".\n"<<endl;
}
【问题讨论】:
-
我在示例代码中没有看到循环。如果 (a > b),那么只需交换 a 和 b。不需要第二个 if。
-
如果 a = b,您的代码也会失败
标签: c++ algorithm division greatest-common-divisor