1. 程式人生 > >C語言訓練-1161-一元二次方程 i

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