10215 - The Largest/Smallest Box ...

这道题10.03%的提交通过率真不是盖的……自己也忽忽悠悠的错了n次……

这道题乍一看挺简单了,刚开始自己先求出极大值和极小值,然后比较,但老是不对。后来发现最小值在0min(l/2, w/2)出取得,而不是0和极小值处=_=

但这样还是不对,在四舍五入的时候要加一个eps偏移量。。。。

这样就可以过了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const double eps = 1e-12;
double w, l;
double sq, root1, root2, a, b;
int sig(double x)
{
    return fabs(x) < eps ? 0 : (x > 0 ? 1 : -1);
}
double fun(double x)
{
    return x * (l - 2 * x) * (w - 2 * x);
}
int main()
{
    //freopen("input.txt", "r", stdin);
    double v1, v2, r;
    while(scanf("%lf %lf", &l, &w) == 2){
        a = l + w;
        b = l * w;
        sq = 4.0 * sqrt(l * l + w * w - l * w);
        root1 = (4 * a - sq) / 24.0;
        r = root2 = min(w / 2.0, l / 2.0);
        v1 = fun(root1);
        v2 = fun(root2);

        if(sig(root1 - root2) < 0){
            if(sig(v1 - v2) == 0){
                printf("%.3lf %.3lf", root1, root2);
            }else if(sig(v1 - v2) > 0)
                printf("%.3lf", root1 + eps);
            else printf("%.3lf", root2 + eps);
        }else printf("%.3lf", root2 + eps);
        root1 = 0;
        root2 = r;
        printf(" %.3lf %.3lf\n", root1 + eps, root2 + eps);
    }
    return 0;
}


发布了113 篇原创文章 · 获赞 12 · 访问量 9万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章