03-1. Two-point method to find polynomial single (20) time limit MS Memory limit 65536 KB code length limit 8000 B award Program StandardAuthor Yang Xifan (City College of Zhejiang University)
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 loop;
This topic requires programming to calculate a given 3-order polynomial f (x) =a3x3+a2x2+ A1x+a0 the root within a given interval [a, b].
input format:
Enter the 4 coefficients A3, A2, A1, A0 of the polynomial in the order given in line 1th, 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:
Outputs the root of the polynomial within the range in a row, exactly 2 digits after the decimal point.
Input Sample: 3-1-3 1-0.5 0.5
Output example: 0.33
Chinese University mooc-Chen, He Chinming-Data structure basic problem sets//03-1. Binomial method for polynomial single-root//create by ZLC on 12/18/1024#include <stdio.h> #include <stdlib.h>double binary_search (double * Coe,double left,double right); int main () {double coe[4]; Double A, B; Double ret; scanf ("%lf%lf%lf%lf", &coe[3],&coe[2],&coe[1],&coe[0]); scanf ("%lf%lf", &a,&b); Ret=binary_search (COE,A,B); printf ("%.2lf\n", ret); return 0;} Double Binary_search (double *coe,double left,double right) {double mid; Double left_res,right_res,mid_res; while (left<=right) {left_res=coe[0]+coe[1]*left+coe[2]*left*left+coe[3]*left*left*left; Right_res=coe[0]+coe[1]*right+coe[2]*right*right+coe[3]*right*right*right; if (left_res*right_res<0) {mid= (left+right)/2; Mid_res=coe[0]+coe[1]*mid+coe[2]*mid*mid+coe[3]*mid*mid*mid; if (mid_res==0) return mid; else{if (right_res*mid_res<0) Left=mid; else Right=mid; }} else if (left_res==0) return left; else if (right_res==0) return right; }}
Chinese University mooc-Chen, He Chinming-Data structure basic problem sets 03-1. Two-part method for finding the polynomial single