为什么这个程序不起作用?这是使用递归函数的简单最大公约数程序。它编译没有错误,但是当我运行 program.exe 时它只是崩溃:“程序已停止工作”。我已经在 codeblocks 和 Notepad++ 上试过了。我使用 gcc 编译器。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int gcd(int,int);
int main(int argc,const char* argv[]){
int a;
int b;
a = atoi(argv[1]);
b = atoi(argv[2]);
printf("The greatest common divisor of %d and %d is %d\n",a,b,gcd(a,b));
return 0;
}
int gcd(int a,int b){
if(a==0)
return a;
else
return gcd(b, a%b);
}
最佳答案
你有这个错误:
if(a==0)
应该是
if(b==0)
你想检查除数不是 0
,不是被除数。
关于c - 简单的GCD程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19102354/