改正这个函数 #include <stdio.h> int MaxCommonFactor(int a, int b); main() { int a, b, x=1; scanf("%d,%d",&a,&b); x = MaxCommonFactor(a, b); if (x < 0) printf("Input Error!\n"); else printf("%d\n", x); } int MaxCommonFactor(int a, int b) { int r=1; if (a <= 0 && b <= 0) return -1; do { r = a % b; b = r; a = b; } while (r != 0); return a; }
时间: 2023-06-26 21:05:06 浏览: 43
#include <stdio.h>
int MaxCommonFactor(int a, int b);
int main() {
int a, b, x=1;
scanf("%d,%d",&a,&b);
x = MaxCommonFactor(a, b);
if (x <= 0)
printf("Input Error!\n");
else
printf("%d\n", x);
return 0;
}
int MaxCommonFactor(int a, int b) {
int r=1;
if (a <= 0 || b <= 0) // 修改判断条件
return -1;
do {
r = a % b;
a = b; // 修正错误
b = r;
} while (r != 0);
return a;
}
主要的修改如下:
1.把 main 函数和 MaxCommonFactor 函数的声明分开
2.在 main 函数中加入 return 0;
3.修改了 if 语句中的判断条件
4.在 MaxCommonFactor 函数中修正了 a 和 b 的赋值顺序。
相关问题
#include <stdio.h> #include <math.h> int main() { int a
#include <stdio.h> 是一个C语言的头文件,它包含了一些输入输出函数的声明,比如printf和scanf等。而#include <math.h> 是一个数学库的头文件,它包含了一些数学函数的声明,比如sqrt和sin等。
在你给出的代码中,int main() 是程序的入口函数,它表示程序从这里开始执行。在main函数中,定义了一个整型变量a,但是没有给它赋初值。
如果你想要使用printf函数来输出一些信息,可以在main函数中添加代码,比如:
int main()
{
int a = 10;
printf("a的值是:%d\n", a);
return 0;
}
这样就可以输出a的值为10。你可以根据需要修改a的值和输出的格式。
#include <stdio.h> int main() { double t
#include <stdio.h> 是C语言中的一个预处理指令,它告诉编译器在编译过程中要包含stdio.h头文件。stdio.h是标准输入输出库的头文件,其中包含了一些常用的输入输出函数的声明。
int main() 是C语言程序的入口函数,程序从这里开始执行。在这个函数中,你可以编写你的程序逻辑。
double t 是一个变量的声明,它的类型是double,表示它可以存储双精度浮点数。这个变量的名字是t。
相关推荐
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)