试题 算法训练 P0104

Lan
Lan
2020-03-24 / 0 评论 / 733 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2020年03月24日,已超过1492天没有更新,若内容或图片失效,请留言反馈。
资源限制
时间限制: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
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

评论 (0)

取消