九度 1551 切蛋糕(数学)

题目描述:

有如下图半价为R的圆形蛋糕,被切一刀后(图中红色直线),分成两个部分(黄色和绿色),已知其比例为r,求刀痕长度(图中红色直线)。

思路

1. 画个图比划比划, 可以看出是道数学题

2. 将刀痕的长度设成 l, 就能建立等式, 接下来就是求 l 了

3. 写成等式后需要以编程的角度出发求解未知数 l, 很自然就能联想到二分法搜索, 但是需要从数学的角度去证明 L 在区间 (0, R) 是单调的

4. 再看图, 发现不需要求导数证明单调, L 越大黄色部分越大, L 越小黄色部分越小, 单调性是 straight forward

5. 对 double 进行二分搜索还是第一次做, 二分后对 low, high 更新时, 我随手取了 0.0001, 觉得精度应该差不多

代码 未通过九度测试

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

const double PI = 3.14159;
double r, l, h;
double theta, portion;

int main() {
    freopen("H:\Copy\workplace_cpp\tt.txt", "r", stdin);

    while(scanf("%lf%lf", &r, &portion) != EOF) {
        double low = 0.00001, high = 2*r;
        while(low <= high) {
            l = (low+high)/2;
            double fff0 = r * r * acos(l/2/r);
            double fff1 = l * sqrt(r*r-l*l/4) / 2;
            double fff2 = PI * r * r / 2 * (1-portion) / (1+portion);
            double fff3 = fff0 + fff1;

            if(fff3 >= fff2) {
                low = l + 0.0001;
            }else{
                high = l - 0.0001;
            }
        }
        printf("%0.2f
", l);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xinsheng/p/3590648.html