标签:printf 比例 jpg 注入 相等 现在 image 技术 mamicode
#include<cstdio> const double eps = 1e-5; double f(double x) { return x * x; } double calSqrt() { double mid,left=1,right=2; while (right - left > eps) { mid = (left + right) / 2; if (f(mid) > 2) right = mid; else left = mid; } return mid; } int main() { printf("%f",calSqrt()); return 0; }
由此可以得出类似这样问题的解法,给定一个定义在[L,R]上的单调函数f(x),求方程法f(x)=0的根
const double eps = 1e-5; double f(double x) { return 函数表达式; } double calSqrt() { double mid, left = 1, right = 2; while (right - left > eps) { mid = (left + right) / 2; if (f(mid) > 2) right = mid;//往左子区间[left,mid]继续逼近 else left = mid; //往右子区间[mid,right]继续逼近 } return mid;//此时与right或left的值相等 }
装水问题:有一个侧面看上去是半圆的储水装置,该半圆的半径是R,要求往里面注入高度为h的水,使得其在侧面看去的面积与b半圆面积S2的比例恰好为r,现在给定R与r,求高度h
#include<cstdio> #include<cmath> const double PI = acos(-1);//acos 是反三角函数 const double esp = 1e-5; double f(double R, double h) { double alpha = acos((R - h) / R); double L = 2 * sqrt((R * R) - (R - h) * (R - h)); double S1 = alpha * R * R - L * (R - h)/2; double S2 = PI * R * R/2; return S1 / S2; } double solve(double R, double r) { double mid,right=R,left=0; while (right - left > esp) { mid = (right + left) / 2; if (f(R, mid) > r) right = mid; else left = mid; } return mid; }
标签:printf 比例 jpg 注入 相等 现在 image 技术 mamicode
原文地址:https://www.cnblogs.com/migang/p/14671711.html