2017-07-26 83 views
1
using namespace std; 
#include <iostream> 
int GCD(int a, int b){ 
    while(b!=0){ 
     int temp=b; 
     b=a%b; 
     a=temp; 
    } 
    return a; 
} 
int main(){ 
    int a=0, b=0; 
    cout<<"Please enter two integers to find their GCD using the Euclidean algorithm."; 
    cin>>a>>b; 
    cout<<"The greatest common divisor of "<<a<<" and "<<b<<" is "<<GCD(a,b)<<"."; 
} 

这是一个简单的C++程序,它可以找到两个数字的最大公约数,它运行得很好。但是,如果我改变GCD程序中奇怪的APPCRASH错误?

int GCD(int a, int b){ 
    while(b!=0){ 

while(a!=0){,我遇到一个错误APPCRASH,异常代码c0000094杀死我跑。我正在运行C++ 11 ISO标准,而我的IDE是Code :: Blocks 16.

+0

*我遇到APPCRASH错误*输入什么?可能是什么时候?b = 0? –

+0

@MichaelWalz如果'b = 0'怎么办? –

+0

@GauravSehgal对不起,我使用了100和150,150和150,以及150和100.这是一个入门级C++课程的简单任务,所以我忽略了异常(以及多么不专业的:P) –

回答

0

您只是有一个“除以0”的错误。

示范:

#include <iostream> 

using namespace std; 

int GCD(int a, int b) { 
    while (a != 0) { 
    int temp = b; 

    if (b == 0) 
    { 
     cout << "Divison by 0\n"; 
     return 0; 
    } 

    b = a % b; 
    a = temp; 
    } 
    return a; 
} 

int main() { 
    int a = 0, b = 0; 
    cout << "Please enter two integers to find their GCD using the Euclidean algorithm."; 
    cin >> a >> b; 
    cout << "The greatest common divisor of " << a << " and " << b << " is " << GCD(a, b) << "."; 
} 

输入:

5 
5 

免责声明:这个程序只证明有一个 “被零除” 的错误,它仍然是不正确的。