[P1034][NOIP2001]一元三次方程求解 (二分)

mac2022-06-30  24

二分

#include<bits/stdc++.h> using namespace std; double a,b,c,d; double fc(double x) { return a*pow(x,3)+b*pow(x,2)+c*x+d; } int main() { scanf("%lf%lf%lf%lf",&a,&b,&c,&d); double l=-100,r=100,ans,x1,x2,mid; int s=0; for(int i=-100;i<=100;i++){ l=i; r=i+1; x1=fc(l); x2=fc(r); if(!x1){ printf("%.2lf ",l); s++; } if(x1*x2<0){ while(r-l>=0.001) { mid=(l+r)/2; if(fc(mid)*fc(r)<=0) l=mid; else r=mid; }printf("%.2f ",r); s++; } if(s==3) break; } return 0; }

 

转载于:https://www.cnblogs.com/lincold/p/9927064.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)