UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#206404#1745. 一元三次方程求解Soulmate1001ms1336kbC++11629b2024-07-22 17:59:502024-07-22 20:03:45

answer

#include <bits/stdc++.h>
using namespace std;


double f(double a,double b,double c,double d,double x) 
{
    return a*x*x*x+b*x*x+c*x+d;
}

int main() 
{
    
    double a,b,c,d;
    cin>>a>>b>>c>>d;

    
    set<double> r;

    
    
    for (double x=-100.00;x<=100.00;x+=0.01) 
    {
        
        if (abs(f(a,b,c,d,x))<1e-6) 
        {
            r.insert(x);
            
            if (r.size()==3) 
            {
                break;
            }
        }
    }

    
    for (double root:r) 
    {
        cout<<fixed<<setprecision(2)<<root<<" ";
    }

    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 25
Accepted
time: 0ms
memory: 1336kb

input:

1 -2 -1 2

output:

-1.00 1.00 2.00 

result:

ok single line: '-1.00 1.00 2.00 '

Test #2:

score: 25
Accepted
time: 0ms
memory: 1336kb

input:

1 -4.65 2.25 1.4

output:

-0.35 1.00 4.00 

result:

ok single line: '-0.35 1.00 4.00 '

Test #3:

score: 25
Accepted
time: 1ms
memory: 1336kb

input:

1 10 -1 -10

output:

-10.00 -1.00 1.00 

result:

ok single line: '-10.00 -1.00 1.00 '

Test #4:

score: 25
Accepted
time: 0ms
memory: 1336kb

input:

1 -1.8 -8.59 -0.84

output:

-2.10 -0.10 4.00 

result:

ok single line: '-2.10 -0.10 4.00 '