C语言二元一次方程组为什么总是无解
#include<stdio.h>#include<math.h>intmain(void){inta,b,c,d,e,f;scanf("%d%d%d%d%d%d",&a...
# include <stdio.h>
# include <math.h>
int main(void)
{
int a, b, c, d, e, f;
scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
double x, y;
double O;
O = a / d;
double X;
X = b / e;
double Z;
Z = c / f;
if (O == X == Z)
{
printf("原方程组有无数的解\n");
}
else if (O == X != Z || O == Z != X || Z == X != O)
{
printf("原方程组无解\n");
}
else
{
x = (c*e - b*f) / (b*d - a*e);
y = (a*f - c*d) / (b*d - a*e);
printf("原方程组有唯一解, x = %f, y = %f\n", x, y);
}
return 0;
}
请问哪里出错了?为什么总是无解? 展开
# include <math.h>
int main(void)
{
int a, b, c, d, e, f;
scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
double x, y;
double O;
O = a / d;
double X;
X = b / e;
double Z;
Z = c / f;
if (O == X == Z)
{
printf("原方程组有无数的解\n");
}
else if (O == X != Z || O == Z != X || Z == X != O)
{
printf("原方程组无解\n");
}
else
{
x = (c*e - b*f) / (b*d - a*e);
y = (a*f - c*d) / (b*d - a*e);
printf("原方程组有唯一解, x = %f, y = %f\n", x, y);
}
return 0;
}
请问哪里出错了?为什么总是无解? 展开
展开全部
# include <stdio.h>
# include <math.h>
int main(void)
{
double a, b, c, d, e, f;
scanf("%lf %lf %lf %lf %lf %lf", &a, &b, &c, &d, &e, &f);
double x, y;
double O;
O = a / d;
double X;
X = b / e;
double Z;
Z = c / f;
if (O == X == Z)
{
printf("原方程组有无数的解\n");
}
else if ((O == X && X != Z) || (O == Z && Z != X) || (Z == X && X != O))
{
printf("原方程组无解\n");
}
else
{
x = (c*e - b*f) / (b*d - a*e);
y = (a*f - c*d) / (b*d - a*e);
printf("原方程组有唯一解, x = %f, y = %f\n", x, y);
}
return 0;
}
你的定义有问题,a,b,c,d,e,f都是整型,但是X,Y,O是浮点型。你又把整型赋值给浮点型是有问题的。这编译的时候可能没错,能够通过,但实际应用时就可能出错。
else if的判断句有问题修改下
(O == X && X != Z) || (O == Z && Z != X) || (Z == X && X != O)
追问
谢谢,
但是唯一解和无解都对了,但是无数解的情况会出现乱码是怎么回事?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询