“浪潮杯”山东省赛 sdut 3256 BIGZHUGOD and His Friends II

题目描述
BIGZHUGOD and his three friends are playing a game in a triangle ground.
The number of BIGZHUGOD is 0, and his three friends are numbered from 1 to 3. Before the game begins, three friends stand on three vertices of triangle in numerical order (1 on A, 2 on B, 3 on C), BIGZHUGOD stands inside of triangle.
Then the game begins, three friends run to the next vertex in uniform speed and in straight direction (1 to B, 2 to C, 3 to A and there speeds may different). And BIGZHUGOD can stand in any position inside the triangle.
When any of his friends arrives at next vertex, the game ends.
BIGZHUGOD and his friends have made an agreement: we assume that the beginning is time 0, if during the game, you can find a moment that BIGZHUGOD can block the sight line of 1 to C, 2 to A, 3 to B. Then each friend has to treat BIGZHUGOD with a big meal.
Now BIGZHUGOD knows the lengths of time that his three friends need run to next vertices t1, t2 and t3. He wants to know whether he has a chance to gain three big meals, of course he wants to know in which exciting moment t, he can block three friends\' sight line.
 
输入
 The first line contains an integer T, indicating the number of test cases (T ≤ 1000).
For each case there are three integer t1, t2, t3 (1 ≤ t1, t2, t3 ≤ 100).
 
输出
 If BIGZHUGOD has a chance to gain big meal from his friends, output "YES" and the exciting moment t rounding to 4 digits after decimal point. Otherwise, output "NO".
示例输入

1 1 1 
3 4 6
示例输出
YES 0.5000 

YES 2.0000


塞瓦定理加二分法求3次方程

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int t1,t2,t3;
        cin>>t1>>t2>>t3;
        int t=min(t1,min(t3,t2));
        double x1=0,x2=t;
        double f1=t1*t2*t3;
        double f2=(t1-x2)*(t2-x2)*(t3-x2)-x2*x2*x2;
        if(f1*f2>0)
        {
            cout<<"NO"<<endl;
            continue;
        }
        else if(f1==0)
        {
            cout<<"YES"<<" ";
            printf("%0.4f\n",x1);
        }
        else if(f2==0)
        {
            cout<<"YES"<<" ";
            printf("%0.4f\n",x2);
        }
        double x0,f0;
        bool flag=0;
        do
        {
            x0=(x1+x2)/2;
            f0=(t1-x0)*(t2-x0)*(t3-x0)-x0*x0*x0;
            if(f0*f1<0)
            {
                x2=x0;
                f2=f0;
            }
            else if(f0*f2<0)
            {
                x1=x0;
                f1=f0;
            }
            else
            {
                cout<<"YES"<<" ";
                printf("%0.4f\n",x0);
                flag=1;
                break;
            }
        }while(fabs(f0)>1e-5);
        if(!flag)
        {
            cout<<"YES"<<" ";
            printf("%0.4f\n",x0);
        }
    }
    return 0;
}


“浪潮杯”山东省赛 sdut 3256 BIGZHUGOD and His Friends II_第1张图片


塞瓦定理是指在△ABC内任取一点O,延长AOBOCO分别交对边于DEF,则 (BD/DC)×(CE/EA)×(AF/FB)=1

即:BD*CE*AF=DC*EA*FB

本题f(x)=(t1-x)(t2-x)(t3-x)-x^3=0;1

求方程在[0,max(t1,max(t2,t3))]的解,误差在1e-5内


用二分法求方程

求方程f=2x^3-4x^2+3x-6=0在区间[-10,10]的解,误差在1e-5的模板

double x1,x2;
    cin>>x1>>x2;
    double f1,f2;
    f1=((2*x1-4)*x1+3)*x1-6;
    f2=((2*x2-4)*x2+3)*x2-6;
    if(f1*f2>0)
        cout<<"无解"<<endl;
    else if(f1==0)
    {
        cout<<x1<<endl;
        return 0;
    }
    else if(f2==0)
    {
        cout<<x2<<endl;
        return 0;
    }
    double x0,f0;
    do
    {
        x0=(x1+x2)/2;
        f0=((2*x0-4)*x0+3)*x0-6;
        if(f0*f1<0)
        {
            x2=x0;
            f2=f0;
        }
        else if(f0*f2<0)
        {
            x1=x0;
            f1=f0;
        }
        else
        {
            cout<<x0<<endl;
            return 0;
        }
    }while(fabs(f0)>=1e-5);//abs是取绝对值后再取整,而fabs是取绝对值之后不做操作
cout<<x0<<endl;




你可能感兴趣的:(塞瓦定理)