计算两个正整数的最大公约数。请按以下给定的函数原型编程:
int MaxCommonFactor(int a, int b);
返回值:返回的是最大公约数;若输入的数据有任意一个不满足条件,返回值是-1。
程序的运行示例1如下:
45,155 (输入,用,隔开)
5 (输出)
程序的运行示例2如下:
-9,20
-1
***输入数据格式***:"%d,%d"
***输出数据格式***:"%d"
1 #include<stdio.h> 2 int MaxCommonFactor(int a, int b); 3 int main(void) 4 { 5 int a, b; 6 scanf("%d,%d", &a, &b); 7 if(a>0&&b>0) 8 printf("%d",MaxCommonFactor(a,b)); 9 else 10 printf("%d",-1); 11 return 0; 12 } 13 int MaxCommonFactor(int a, int b) 14 { 15 int i; 16 while (b != 0) 17 { 18 i = a % b; 19 a = b; 20 b = i; 21 } 22 return a; 23 }
原文:https://www.cnblogs.com/20201212ycy/p/14589630.html