回复 9楼 beyondyf
用牛顿迭代法怎么算的,圆台体积公式是关于高度的一次函数啊,求导出来不就是常数了吗?[ 本帖最后由 waterstar 于 2011-12-3 20:55 编辑 ]
冰冻三尺,非一日之寒;士别三日,不足刮目相看!
#include<stdio.h> #define PI 3.1415926535897932384626433832795 #define MIN 0.0000001 double a, b, c, d; double da, db, dc; double f(double x) { double xx, xxx; xx = x * x; xxx = xx * x; return a * xxx + b * xx + c * x + d; } double df(double x) { return da * x * x + db * x + dc; } void init(double r, double R, double H, double V) { double k; k = (R - r) / H; a = k * k / 3; b = k * r; c = r * r; d = -V / PI; da = a * 3; db = b * 2; dc = c; } double cal(double r, double R, double H, double V) { double x0, x1; init(r, R, H, V); x0 = 0; x1 = x0 - f(x0) / df(x0); while(fabs(x1 - x0) >= MIN) { x0 = x1; x1 = x0 - f(x0) / df(x0); } return x1; } int main() { int T; double r, R, H, V, h; scanf("%d", &T); while(T--) { scanf("%lf%lf%lf%lf", &r, &R, &H, &V); h = cal(r, R, H, V); if(h > H) h = H; printf("%.6f\n", h); } return 0; }