一元二次方程求解问题
本帖最后由 扶风之木 于 2022-6-6 22:10 编辑原题如图:https://s1.ax1x.com/2022/06/06/XBpH10.png
#include<stdio.h>
#include<math.h>
int main()//解一元二次方程
{
float a, b, c,t,x1,x2;
printf("Please input a,b,c:");
scanf_s("%lf,%lf,%lf",&a, &b, &c);
t = b * b - 4 * a * c;
if (t > 0)
{
x1 = -b + sqrt(t) / 2 * a, x2 = -b - sqrt(t) / 2 * a;
printf("%f,%f", x1, x2);
else if (t == 0)
{
x1 = x2 = -b / 2 * a; printf("%f,%f", x1, x2);
}
else printf("方程无解");
}
} 图呢?问题呢? #include <stdio.h>
#include <math.h>
int main()
{
float a,b,c,x1,x2,d;
printf("Please input a,b,c:");
scanf("%f %f %f",&a,&b,&c);
if(a!=0)
{
d=sqrt(b*b-4*a*c);
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
if(x1<x2)
printf("%0.2f %0.2f\n",x2,x1);
else
printf("%0.2f %0.2f\n",x1,x2);
}
return 0;
} 风车呼呼呼 发表于 2022-6-6 21:06
图呢?问题呢?
以编辑谢谢
目前自己改成了这样
#include<stdio.h>
#include<math.h>
int main()//解一元二次方程
{
double a, b, c,t;
int x1,x2;
printf("Please input a,b,c:");
scanf_s("%lf,%lf,%lf",&a, &b, &c);
t = (b * b) - (4 * a * c);
if (t > 0)
printf("%d,%d", x1 = (-b + sqrt(t)) / (2 * a), x2 = (-b - sqrt(t)) / (2 * a));
else if (t == 0)
printf("%d,%d", x1 = -b / (2 * a), x2 = -b / (2 * a));
else printf("方程无解");
} 临时号 发表于 2022-6-6 21:27
谢谢 临时号 发表于 2022-6-6 21:27
大佬可以指一下我的问题吗谢谢
页:
[1]