C语言训练-1161-一元二次方程 i

Problem Description
解一元二次方程ax2+bx+c=0的解。保证有解
Input
a,b,c的值。
Output
两个根X1和X2,其中X1>=X2。 结果保留两位小数。
Sample Input
1 5 -2
Sample Output
0.37 -5.37
代码如下,没什么难度,把公式套上就行了

#include <stdio.h>
#include <math.h>
void f(double a, double b, double c)
{
    double x1, double x2, double t;
    if(b*b-4*a*c>=0)
    {
    	x1=(-b+sqrt(b*b-4*a*c))/(2*a);
    	x2=(-b-sqrt(b*b-4*a*c))/(2*a);
	}
	if(x1<x2)
	{
		t=x1;
		x1=x2;
		x2=t;
	}
		printf("%.2lf %.2lf\n",x1,x2);
		return ;
}

int main()
{
	double a,b,c;
	scanf("%lf %lf %lf",&a,&b,&c);
    f(a,b,c);
	return 0;
}

老规矩链接如下
https://blog.csdn.net/Lycodeboy/article/details/53046192?locationNum=2&fps=1

猜你喜欢

转载自blog.csdn.net/weixin_43450493/article/details/83537947
今日推荐