Poj 2707: finding the root of a quadratic equation

Source: Internet
Author: User
Using formula X1 = (-B + SQRT (B * B-4 * a * C)/(2 * ), x2 = (-B-SQRT (B * B-4 * a * C)/(2 * A) returns the root of the quadratic equation ax2 + bx + c = 0, A is not equal to 0.

There are two main points to note in this question: 1. To understand the calculation of the complex number, we need to consolidate the virtual number to be learned in high school; 2. It is the limit hypothesis for the double value.

AC code:

1 # include <stdio. h> 2 # include <math. h> 3 4 int main () 5 {6 int N; 7 scanf ("% d", & N); 8 while (n --) 9 {10 double A, B, c; 11 double x1, x2; 12 scanf ("% lf", & A, & B, & C ); 13 Double Delta = B * B-4 * a * C; 14 if (delta> 0) 15 {16x1 = (-B + SQRT (DELTA )) /(2 * A); 17x2 = (-B-SQRT (DELTA)/(2 * A); 18 printf ("X1 = %. 5lf; x2 = %. 5lf \ n ", x1, x2); 19} 20 else if (FABS (DELTA) <1e-8) // when the absolute value of the double variable is extremely small, when it is smaller than the precision range, we think it is zero 21 {22x1 =-B/(2 * A); 23 printf ("X1 = x2 = %. 5lf \ n ", X1); 24} 25 else26 {27 Delta = 0-delta; // calculate the negative Delta value by 28 Double P =-B/(2 * A); 29 double Q = SQRT (DELTA)/(2 * ); 30 if (FABS (p) <1e-8) P = 0.0; // for negative zero detection 31 printf ("X1 = %. 5lf + %. 5lfi; x2 = %. 5lf-%. 5lfi \ n ", p, q, p, q); 32} 33} 34 return 0; 35}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.