C语言编写两个函数,分别求其最大公约数和最小公倍数。并在主函数中输入任意两个正整数m和n,调用前两个函
答案:5 悬赏:80 手机版
解决时间 2021-04-04 18:37
- 提问者网友:欲劫无渡
- 2021-04-04 00:47
C语言编写两个函数,分别求其最大公约数和最小公倍数。并在主函数中输入任意两个正整数m和n,调用前两个函
最佳答案
- 五星知识达人网友:患得患失的劫
- 2021-04-04 01:10
#include
void main()
{int a,b,num1,num2,temp;
scanf("%d %d",&num1,&num2);
if(num1 a=num1;b=num2;
while(b!=0)
{temp=a%b;
a=b;
b=temp;}
printf("%d\n",a);
printf("%d\n",num1*num2/a);
}
void main()
{int a,b,num1,num2,temp;
scanf("%d %d",&num1,&num2);
if(num1
while(b!=0)
{temp=a%b;
a=b;
b=temp;}
printf("%d\n",a);
printf("%d\n",num1*num2/a);
}
全部回答
- 1楼网友:轻熟杀无赦
- 2021-04-04 03:52
#include "stdio.h"
void main()
{
int i,j,k,m;
scanf("%d%d",&i,&j);
k=gy(i,j);
m=gb(i,j);
printf("最大公约数为%d\n",k);
printf("最大公倍数为%d\n",m);
}
int gy(int a,int b)
{
int temp,r;
if(a{
temp=a;a=b;b=temp;
}
r=a%b;
while(r!=0)
{
a=b;b=r;
r=a%b;
}
return b;
}
int gb(int a,int b)
{int m;
m=a*b/gy(a,b);
return m;}
void main()
{
int i,j,k,m;
scanf("%d%d",&i,&j);
k=gy(i,j);
m=gb(i,j);
printf("最大公约数为%d\n",k);
printf("最大公倍数为%d\n",m);
}
int gy(int a,int b)
{
int temp,r;
if(a{
temp=a;a=b;b=temp;
}
r=a%b;
while(r!=0)
{
a=b;b=r;
r=a%b;
}
return b;
}
int gb(int a,int b)
{int m;
m=a*b/gy(a,b);
return m;}
- 2楼网友:轻雾山林
- 2021-04-04 03:03
#include
void haha();
int main()
{
int m,n;
int i,j,k,l;
int yueshu,beishu;
printf("输入两个整数,\n");
scanf("%d%",&m);
scanf("%d%",&n);
if(m>n)
haha(m,n)
else
haha(n,m)
beishu=m*n/yueshu;
printf("%d与%d最大公约数%d",m,n,yueshu);
printf("%d与%d的最小公倍数%d",m,n,beishu);
return 0;
}
void haha(a,b)
{
int i,j,k;
while(k=0)
{
k=a mod b;
a=b;
b=k;
}
}
void haha();
int main()
{
int m,n;
int i,j,k,l;
int yueshu,beishu;
printf("输入两个整数,\n");
scanf("%d%",&m);
scanf("%d%",&n);
if(m>n)
haha(m,n)
else
haha(n,m)
beishu=m*n/yueshu;
printf("%d与%d最大公约数%d",m,n,yueshu);
printf("%d与%d的最小公倍数%d",m,n,beishu);
return 0;
}
void haha(a,b)
{
int i,j,k;
while(k=0)
{
k=a mod b;
a=b;
b=k;
}
}
- 3楼网友:山河有幸埋战骨
- 2021-04-04 02:37
#include
void main()
{int a,b,num1,num2,temp;
scanf("%d %d",&num1,&num2);
if(num1 a=num1;b=num2;
while(b!=0)
{temp=a%b;
a=b;
b=temp;}
printf("%d\n",a);
printf("%d\n",num1*num2/a);
}
void main()
{int a,b,num1,num2,temp;
scanf("%d %d",&num1,&num2);
if(num1
while(b!=0)
{temp=a%b;
a=b;
b=temp;}
printf("%d\n",a);
printf("%d\n",num1*num2/a);
}
- 4楼网友:笑迎怀羞
- 2021-04-04 01:15
#include
int MaxCommonFactor(int x,int y);
void main()
{
int a,b,s;
printf("input two integers a and b\n");
scanf("%d%d",&a,&b);
s=MaxCommonFactor(a,b);
printf("the highest common factor between a and b is %d\n",s);
}
int MaxCommonFactor(int x,int y)
{
int i;
if(x {
for(i=x;i>=1;i--)
{
if((x%i==0)&&(y%i==0))
return i;
else continue;
}
}
else if(x>y)
for(i=y;i>=1;i--)
{
if((x%i==0)&&(y%i==0))
return i;
else continue;
}
else return x;
}
已经调试通过
int MaxCommonFactor(int x,int y);
void main()
{
int a,b,s;
printf("input two integers a and b\n");
scanf("%d%d",&a,&b);
s=MaxCommonFactor(a,b);
printf("the highest common factor between a and b is %d\n",s);
}
int MaxCommonFactor(int x,int y)
{
int i;
if(x
for(i=x;i>=1;i--)
{
if((x%i==0)&&(y%i==0))
return i;
else continue;
}
}
else if(x>y)
for(i=y;i>=1;i--)
{
if((x%i==0)&&(y%i==0))
return i;
else continue;
}
else return x;
}
已经调试通过
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯