试题 算法训练 P0104

2022-07-13 18:52:06 浏览数 (1)

资源限制

时间限制:1.0s 内存限制:256.0MB

  求方程ax2 bx c=0的实数根。a, b, c由键盘输入, a!=0。若只有一个实数根(b2-4ac=0)则只输出x1,若无实数根(b2-4ac<0)则输出Error。 输入   2.5 7.5 1.0 输出 (注意等号前面后面都有一个空格)   x1 = -0.139853   x2 = -2.860147

代码语言:javascript复制
import java.util.*;

public class P0104 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		double a = sc.nextDouble();
		double b = sc.nextDouble();
		double c = sc.nextDouble();
		double x = b * b - 4 * a * c;
		if (x < 0) {
			System.out.println("Error");
		} else if (x == 0) {
			double result = (-b - Math.sqrt(x)) / (2 * a);
			System.out.printf("x1 = %.6f 
",result);
		} else {
			double result1 = (-b   Math.sqrt(x)) / (2 * a);
			double result2 = (-b - Math.sqrt(x)) / (2 * a);
			System.out.printf("x1 = %.6f 
",result1);
			System.out.printf("x2 = %.6f",result2);
		}
	}

}

0 人点赞