Java implementation of the Blue Bridge Cup VIP algorithm to improve the Quadratic Equation

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Quadratic Equation algorithm improves
time limit: 1.0s memory limit: 512.0MB
description of the problem
  solving equations ax2 + bx + c = 0 the roots. Claim a, b, c input by the user, and may be any real number.
  Input format: single line input, comprising three coefficients, by spaces between open.
  Output formats: Output only one line, including two roots, the root front, small roots in the post, regardless of special circumstances, to two decimal places.
  Sample input and output
sample input
2.5 7.5 1.0
Sample Output
-0.14 -2.86

import java.util.Scanner;


public class QuadraticEquation {
	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 x1=(-b+Math.pow(b*b-4*a*c, 0.5))/(2*a);
		double x2=(-b-Math.pow(b*b-4*a*c, 0.5))/(2*a);
		if(x1>x2){
			System.out.printf("%.2f %.2f",x1,x2);
		}else{
			System.out.printf("%.2f %.2f",x2,x1);
		}
	}

}

Guess you like

Origin blog.csdn.net/a1439775520/article/details/93307784