Resolve the problem by using the binary method.
This question is disgusting... None of the test examples can be passed at the beginning. If this π is obtained at the beginning, the result is 3.1415926, Which is changed to 99.999026, and the result is 3.1414927 .. I found that this π has a great impact on the results, and this question has a high requirement on the accuracy of π... Then I changed my π to 3.1415926536...
The first condition for determining the exit of the binary is that the result difference is less than 0.000000001. If the result is found to have timed out, The result still times out after it is changed to 0.01... Speechless... Then think about it. The question requires that the result be retained to 6 decimal places, so I changed my judgment to jump out of the cycle.
The condition of the ring is changed to the result after binary and the result difference between the previous result is not greater than 0.000001 ....
AC code:
# Include <iostream> # include <cstdio> # define pai 3.1415926536 using namespace std; int main () {int t; double r, R, h, v, vol, nowh, left, right, nowr, lath; scanf ("% d", & t); while (t --) {scanf ("% lf ", & r, & R, & h, & v); lath = left = 0.0; right = h; while (1) {nowh = (left + right)/2; if (lath <nowh) {if (nowh-lath <0.0000001) // judge whether the difference between the current value and the previous value is less than 0.000001, then exit {break ;}} else {if (Lath-nowh <0.0000001) {break ;}} nowr = nowh * (R-r)/h + r; vol = pai * nowh * (r * r + nowr * nowr)/3; if (vol> v) {right = nowh ;} else {left = nowh;} lath = nowh; // record the last value} printf ("%. 6lf \ n ", nowh);} return 0 ;}