[二分] [洛谷] P1258 小车问题

浮点二分 加点要比eps小

//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;

const int MAXN = 1e6 + 10;

const double eps = 1e-8;

double len, a, b;

int cal(double point)
{
    double t1, t2, t3, t4, s1, s2, s3, s4, st1, st2;
    t1 = point / b;
    s1 = point - t1 * a;
    t2 = s1 / (a + b);
    s2 = len - point + t2 * b;
   	
    st1 = t1 + t2 + s2 / b;
    st2 = t1 + (len - point) / a;

    if(fabs(st1 - st2) < eps)
    {
        cout<<fixed<<setprecision(6)<<st1<<endl;
        return 1;
    }
    
    else if(st1 - st2 > 0)
        return 2;
    else
        return 3;
        
}
int main()
{
    //ios::sync_with_stdio(false);

    //cin.tie(0);     cout.tie(0);

    

    cin>>len>>a>>b;

    double fst = 0, lst = len, mid;

    while(lst - fst > -eps)
    {
        mid = (fst + lst) / 2;

        double ct = cal(mid);

        if(ct == 1)
        {
            break;
        }
        else if(ct == 2)
        {
            lst = mid - eps;
        }
        else if(ct == 3)
        {
            fst = mid + eps;
        }   
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Zeolim/article/details/82832249
今日推荐