急!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11
// This program demonstrates a recursive function to
// calculate the greatest common divisor (gcd) of two numbers.
#include <iostream>
using namespace std;
// Function prototype
int gcd(int, int);
int main()
{
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
cout << "The greatest common divisor of " << num1;
cout << " and " << num2 << " is ";
cout << gcd(num1, num2) << endl;
return 0;
}
int gcd(int x, int y)
{
if (x % y == 0) //base case
return y;
else
return gcd{y, x % y);
}
#include <iostream>
using namespace std;
// Function prototype
int gcd(int a, int b){
int r = a % b; //余数
int q = a / b; //商''''''
if (r == 0)
return b;
else
return gcd(b,r);
}
int main(){
int a,b;
cin>>a>>b;
cout << gcd(a,b);
return 0;
}
#include <stdio.h>
int gcd(int a,int b)
{
if(a<=0 || b<=0) return 0; //如果传进来的参数小于0则返回0
if(a%b==0) return b; //如果b能被a整除则b就是最大公约数
else gcd(b,a%b); //递归 调用函数本身
}
int main()
{
int a,b,c;
printf("请输入两个数:\n");
scanf("%d,%d",&b,&c);
a=gcd(b,c);
printf("%d和%d的最大公约数为:%d\n",b,c,a);
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
其实可以用C++自带函数__gcd,具体用法: