#include <stdio.h> #include <math.h> double P, Q, K, X; //定义全局变量 void d0(); void d1(); void d2(); //声明自定义函数 int main() { double a, b, c; scanf("%lf %lf %lf", &a, &b, &c); P = b * b - 4 * a * c; //根的判别式:▲=b^2-4ac Q = (-b) / (2 * a); //-b/(2a) K = 2 * a; X = sqrt(fabs(P)) / K; //√(b^2-4ac)/2a if (P < 0) { d0(); } if (P == 0) { d1(); } if (P > 0) { d2(); } return 0; } void d0() { //▲<0时,方程无实数根,但有2个共轭复根 printf("x1=%.3lf+%.3lfi x2=%.3lf-%.3lfi", Q, X, Q, X); } void d1() { //▲=0时,方程有两个相等的实数根 printf("x1=%.3lf x2=%.3lf", Q + X, Q + X); } void d2() { //▲ > 0 时,方程有两个不相等的实数根 printf("x1=%.3lf x2=%.3lf", Q + X, Q - X); }
import java.util.Scanner; public class Main { static double P, Q, K, X; //定义全局变量 public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = sc.nextDouble(); double b = sc.nextDouble(); double c = sc.nextDouble(); P = b * b - 4 * a * c; //根的判别式:▲=b^2-4ac Q = (-b) / (2 * a); //-b/(2a) K = 2 * a; X = Math.sqrt(Math.abs(P)) / K; //√(b2-4ac)/2a if (P < 0) { d0(); } else if (P == 0) { d1(); } else { d2(); } } static void d0() { //▲<0时,方程无实数根,但有2个共轭复根 System.out.printf("x1=%.3f+%.3fi x2=%.3f-%.3fi", Q, X, Q, X); } static void d1() { //▲=0时,方程有两个相等的实数根 System.out.printf("x1=%.3f x2=%.3f", Q + X, Q + X); } static void d2() { //▲ > 0 时,方程有两个不相等的实数根 System.out.printf("x1=%.3f x2=%.3f", Q + X, Q - X); } }
0.0分
0 人评分