1/* 2 * Main. C 3 * C8-loop-08. returns a single polynomial using the bipartite method. 4 * created on: July 26, 2014 * Author: boomkeeper 6 * is partially implemented through * 7 */8 # include <stdio. h> 9 # include <math. h> 10 11 float a3 = 0, a2 = 0, a1 = 0, a0 = 0; 12 13 double func (Double X) {14 return (A3 * POW (X, 3) + A2 * POW (x, 2) + A1 * x + a0); 15} 16 17 18 int main (void) {19 float a = 0, B = 0; // interval 20 float midvalue = 0; // midvalue = (a + B)/221 float threshold = 0; 22 23 scanf ("% F", & A3, & A2, & A1, & A0, &, & B); 24 25 do {26 midvalue = (a + B)/2; // calculate the median 27 if (func (a) * func (B) <0) {28 If (func (midvalue) = 0) {29 printf ("%. 2f \ n ", midvalue); 30 return 0; 31} else {32 If (func (midvalue) * func (a)> 0) {33 // same number, the description is left. 34 A = midvalue; 35 Threshold = FABS (a-B); // calculates the threshold value 36 continue; 37} 38 If (func (midvalue) * func (B)> 0) {39 // same number, indicating that it is right and should be 4 to the left 0 B = midvalue; 41 Threshold = FABS (a-B); // calculate the range threshold of 42 continue; 43} 44} 45} 46} while (threshold> 0.0001 ); 47/** 48 * after threshold is small to a certain extent, it indicates that a and B can be viewed as one point, that is, the R point where F (R) = 0 in the question. 49 */50 printf ("%. 2f \ n", (A + B)/2); 51 52 return 0; 53}
For those who have better methods, I hope to give some advice. I tried to increase the accuracy of 1 while (threshold> 0.0001). The test results are still the same. I think there may still be problems with the algorithm.
Question link:
Http://pat.zju.edu.cn/contests/basic-programming/%E5%BE%AA%E7%8E%AF-08
* Loop-08. Finding a single polynomial using the bipartite Method