pat-"c/c++/java/pascal Program design Basics" Problem sets-loop -08 (mooc3-1) Two-part method for finding polynomial single

Source: Internet
Author: User

First, stick to the topic:

The principle of the dichotomy method is: if the continuous function f (x) in the interval [a, b] two endpoints of the value of the difference, that is F (a) f (b) <0, then it has at least 1 root R in this interval, that is, f (r) = 0.

The steps of the dichotomy are:

Check the interval length, if it is less than the given threshold, stop, the output interval midpoint (a+b)/2;
If f (a) f (b) <0, the value of the midpoint is computed F ((A+B)/2);
If f ((A+B)/2) is exactly 0, then (a+b)/2 is the root of the requirement;
If f ((A+B)/2) is the same as F (a), then the root is in the interval [(A+B)/2, b], making a= (A+B)/2, repeating the cycle;
If f ((A+B)/2) is the same as F (b), then the root is in the interval [A, (A+B)/2], making b= (A+B)/2, repeating the cycle;

This topic requires a program to calculate the root of a given 3-order polynomial f (x) =a3x3+a2x2+a1x+a0 within a given interval [a, b].

Input format:

Enter the 4 coefficients A3, A2, A1, A0 of the polynomial in the order given in the 1th row, giving the interval endpoints a and B sequentially in line 2nd. The topic guarantees that the polynomial has a unique single root within a given interval.

Output format:

The root of the polynomial within the interval is output in a row, which is exactly 2 digits after the decimal point.

Input Sample:
3-1-3 1
-0.5 0.5
Sample output:
0.33

Precautions:

1. The title is required to be accurate to 2 digits after the decimal point, so a constant of # define or const, #define EPS 1e-3 or const double eps=1e-3. Use this constant to determine the size of the interval. That is, when (a, b) interval length b-a is less than or equal to 1e-3, terminate the loop.

2. During the cycle, if f ((A+B)/2) equals 0, the loop is terminated prematurely and the result is output. Judging from the outside of the loop if F ((A+B)/2) is not equal to 0, it means that there is no result output in the loop body, but because the interval length is too small (less than or equal to 1e-3) so that the loop terminates, at which time to output (A + B)/2.

3. Function calculation, using x * (x * (A3 * x + A2) + A1) + a0 is better than a3*x*x*x+a2*x*x+a1*x+a0!

Because this problem is relatively simple, the notice has been explained in advance, so the direct adhesive code:

1#include <iostream>2#include <cstdio>3 4 #defineEPS (1e-3)//Precautions 15 using namespacestd;6 7 DoubleA3, A2, A1, A0;8 DoubleFuncDoublex);9 intMain ()Ten { One     DoubleA, B; ACIN >> A3 >> A2 >> A1 >>A0; -Cin >> a >>b; -  the     DoubleFA =func (a); -     DoubleFB =func (b); -      while(B-a > EPS)//Precautions 1 -     { +         DoubleMid = (A + b)/2; -         DoubleFmid =func (mid); +         if(Fmid = =0)//Precautions 2 A         { atprintf"%.2LF", mid); -              Break; -         } -         if(FA * Fmid >0) -         { -A =mid; inFA =func (a); -             Continue; to         } +         if(FB * fmid >0) -         { theb =mid; *FB =func (b); $             Continue;Panax Notoginseng         } -     } the     if(Func (A + B)/2) !=0) +printf"%.2f\n", (A + B)/2);//Precautions 2 A  the     return 0; + } - DoubleFuncDoublex) $ { $     returnX * (x * (A3 * x + A2) + A1) + a0;//Precautions 3 -}

Stick to the usual results of AC:

pat-"c/c++/java/pascal Program design Basics" Problem sets-loop -08 (mooc3-1) Two-part method for finding polynomial single

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.