有没有哪位帅哥能看出这写的有什么问题吗
#include <stdio.h>int main()
{
int gcd(int n1,int n2);
int lcm(int m1,int m2);
int x,y,m,n;
printf("please enter two numbers: \n");
scanf("%d %d",&x,&y);
m=gcd(x,y);
n=lcm(x,y);
printf("%d和%d的最大公约数为%d\n",x,y,m);
printf("%d和%d的最小公倍数为%d\n",x,y,n);
}
int gcd(int n1,int n2)
{
int middle,max,min;
max=n1 > n2 ? n1:n2;
min=n1 < n2 ? n1:n2;
middle = max % min;
max = min;
min = middle;
return max;
}
int lcm(int m1,int m2)
{
int t;
t=m1*m2/gcd(int n1,int n2);
return t;
} 本帖最后由 qiuyouzhi 于 2021-7-22 16:45 编辑
1,函数声明通常放在全局
2,函数调用时只需传参数就好了,你貌似不太理解形参和实参,可以百度下
3,你的 gcd 函数中应该有个循环呀
#include <stdio.h>
int gcd(int n1,int n2);
int lcm(int m1,int m2);
int main()
{
int x, y, m, n;
printf("please enter two numbers: \n");
scanf("%d %d",&x,&y);
m=gcd(x,y);
n=lcm(x,y);
printf("%d和%d的最大公约数为%d\n",x,y,m);
printf("%d和%d的最小公倍数为%d\n",x,y,n);
}
int gcd(int n1,int n2)
{
int tmp, max ,min;
max = n1 > n2 ? n1:n2;
min = n1 < n2 ? n1:n2;
while (min)
{
tmp = max % min;
max = min;
min = tmp;
}
return max;
}
int lcm(int m1,int m2)
{
int t;
t = m1 * m2 / gcd(m1, m2);
return t;
} 谢谢大哥! antifa 发表于 2021-7-22 17:24
谢谢大哥!
好人一生平安
页:
[1]