C语言程序没有错误,但是运行不了。

#include<stdio.h>
int r;
int main()
{
int maxa(int,int);
int minb(int,int);
int a,b,max,min;
printf("please enter two integers:");
scanf("%d,%d",&a,&b);
max=maxa(a,b);
printf("the maxa is:%d",max);
min=minb(a,b);
printf("the minb is:%d",min);
return 0;
}
int maxa(int a,int b)
{
int t;
if(a<b)
{t=a;a=b;b=t;}
while((r=a%b)!=0)
{ a=b;b=r;}
return b;
}
int minb(int a,int b)
{
return (a*b/r);
}

这个程序中最大公约数没有问题可以出来,但是后面的最小公倍数就出不来。程序没有报错。我也不知道是哪里错了。。。。。。估计是余数r的问题,用全局变量不行吗?为什么第二个小函数里r就不起作用了呢?

最好按以下步骤:
1.f9
2.ctrl
+
f9
3.alt
+
f5
如果不能保证程序没有错误,最好先用f9,如果把握行比较大,直接
ctrl
+
f9
,不要第一步。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-08-08
你的代码是错了,所以才会这样。错误的地点在输出上,也就是
最后一句
printf("%d %c %s", ss.age, ss.name, ss.sex);你定义的name是字符串,而sex是字符。所以应该改成
printf("%d %s %c", ss.age, ss.name, ss.sex);
第2个回答  2012-11-18
r都成0了才退出循环,在第二个函数中它就是非法操作了,0作,算法错
给你一个正确的:
#include<stdio.h>
int maxaminb(int a,int b,int*maxa,int*minb)
{
int c,d,e;
if(a<1||b<1||!maxa||!minb)
return 0;//参数错误
if(b>a) {c=a; d=b%a;}
else {c=b; d=a%b;}
while (d) //
{
e=c; c=d; d = e%d;
}
*maxa = c;
*minb = a*b/c;
return 1;
}
int main()
{
int a,b,c,d;
scanf("%d%d",&a,&b);
if(!maxaminb(a,b,&c,&d))
return 0;
printf("%d与%d %d %d",a,b,c,d);
return 0;
}本回答被提问者采纳
第3个回答  2012-11-18
int minb(int a,int b)
{
return (a*b/r);
}

r没有定义追问

我定义了啊。。。主函数里面写了呀。。。

第4个回答  2020-02-05
先初始化变量试试

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网