Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2289
The main idea is a cup, a round table shape, give you its top circle radius, the radius of the bottom circle, the height of the cup, and at this time the volume of water installed, to seek the height of water.
The problem is to exercise algorithmic thinking, or to form algorithmic thinking.
This is an application of two-point approximation evaluation
The topic has been given the height of the cup is 0~100, so in this range of two points, but went to the question of a decimal point, it is possible to take a higher precision error value
As a condition of two points, two minutes, with this value as the height of water to find out the volume and the actual value of the comparison once, when the error is not greater than the error value, then take as high.
Another round Table volume formula: Set the radius of the bottom is r, the radius of the bottom is R, the height is H v= (1/3) *pi*h* (r^2 + Rr +r^2) pi
1#include <stdio.h>2#include <math.h>3 DoublePi=acos (-1.0);//Pi4 Doublehaha =0.000000001;//take the error value5 6 DoubleYjDoubleRDoubleRDoubleHDoubleH)7 {8 DoubleU = h/h* (r-r) + R;//find out the radius of the water circle9 returnpi/3* (R*r+r*u+u*u) *h;//returns the volume of the Circular water tableTen } One A DoublelsDoubleRDoubleRDoubleHDoublev) - { - DoubleLeft,right,temp,mid; theleft =0, right = -; - while((right-left) >haha)//the binary condition becomes the comparison with the error value - { -Mid = (left+right)/2; +temp=YJ (r,r,mid,h); - if(Fabs (TEMP-V) <=haha) + returnmid; A Else if(temp<v) atLeft=mid+haha; - Else -right = mid-haha; - } - return(Right+left)/2; - } in - intMain () to { + intT; - Doubler,r,h,v; the while(~SCANF ("%d",&t)) { * while(t--) $ {Panax Notoginsengscanf"%LF%LF%LF%LF",&r,&r,&h,&v); -printf"%.6lf\n", LS (r,r,h,v)); the } + } A return 0; the}
HUD 2289 Cup with two points