- 一元三次方程求解
- 2017-10-22 22:04:58 @
#include<bits/stdc++.h>
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
double a,b,c,d;
void search(double x1,double x2)
{
double ans;
double y1 = a*x1*x1*x1 + b*x1*x1 + c*x1 + d;
double y2 = a*x2*x2*x2 + b*x2*x2 + c*x2 + d;
if(y1 * y2 < 0 && x1!=x2)
{
x1 += 0.01;
}
x1 -= 0.01;
ans = x1;
cout << ans << " ";
}
int main()
{
/* freopen("1116.in","r",stdin);
freopen("1116.out","w",stdout);*/
double x1 = -100.00,x2 = -99.00; //x1为head,x2为tail ,x2>x1,f(x2)*f(x1)<0
int y1 = 0 ,y2 = 0;
int amount = 0,i = 0; //ans个数
cin >> a >> b >> c >> d;
y1 = a*x1*x1*x1 + b*x1*x1 + c*x1 + d;
y2 = a*x2*x2*x2 + b*x2*x2 + c*x2 + d;
while(amount < 3)
{
if(y1 * y2 < 0)
{
x1 = x2 - 1;
search(x1,x2);
amount ++;
x2++;
x1++;
}
else
{
x2++;
y2 = a*x2*x2*x2 + b*x2*x2 + c*x2 + d;
}
}
/* fclose(stdin);
fclose(stdout);*/
return 0;
}
枚举错在哪啊