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 ++程序,可以找到两个数字的最大公约数,它运行得很好。但是,如果我改变
int GCD(int a, int b){
while(b!=0){
进入while(a!=0){
,我遇到APPCRASH错误,异常代码c0000094会导致我的运行失败。我正在运行C ++ 11 ISO标准,我的IDE是Code :: Blocks 16。
答案 0 :(得分: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
免责声明:此程序仅显示存在“除以0”错误,但仍然不正确。