题目描述
求解方程ax2+bx+c=0的根。要求a, b, c由用户输入,并且可以为任意实数。
输入
输入只有一行,包括三个系数,之间用空格格开。
输出
输出只有一行,包括两个根,大根在前,小根在后,无需考虑特殊情况,保留小数点后两位。
样例输入
2.5 7.5 1.0
样例输出
-0.14 -2.86
package parctice;
import java.util.Scanner;
public class ph1484 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//输入三个数
double a=sc.nextDouble();
double b=sc.nextDouble();
double c=sc.nextDouble();
double beita=b*b-4*a*c;
double x1=(-b+Math.sqrt(beita))/(2*a);
double x2=(-b-Math.sqrt(beita))/(2*a);
//判断大小
if (x1<x2) {
double temp=x1;
x1=x2;
x2=temp;
System.out.println(String.format("%.2f", x1)+" "+String.format("%.2f", x2));
}else {
System.out.println(String.format("%.2f", x1)+" "+String.format("%.2f", x2));
}
}
}