直接写
#include<iostream> #include<cmath> #include<iomanip> using namespace std; double n; int main() { bool sign; cin>>n; double l=-10000,r=10000; while(r-l>=1e-8) { double mid=(l+r)/2; if(mid*mid*mid>=n) r=mid; else l=mid; } cout<<setiosflags(ios::fixed)<<setprecision(6)<<l; return 0; }
函数有错需要改
#include<iostream> #include<cmath> #include<iomanip> using namespace std; double n; double dfs(int l,int r,int n) { double mid=(l+r)/2; if(r-l<1e-1) return mid; else { if(mid*mid*mid>=n) return dfs(l,mid,n); else return dfs(mid,r,n); } } int main() { cin>>n; double l=-10000,r=10000; cout<<setiosflags(ios::fixed)<<setprecision(6)<<dfs(l,r,n); return 0; }
标签:二分,10000,int,double,mid,dfs,方根,查找,include From: https://www.cnblogs.com/weinan030416/p/17067832.html