题目描述
给定一个实数 y,求 y 的三次方根。
为什么这个不行(40 TLE)。
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double y;
double f_(double x){return 3*x*x;}
double f(double x){return x*x*x-y;}
double j(){
double x=y/3;
while(fabs(f(x))>0.001) x=x-f(x)/f_(x);
return x;
}
int main(){
cin>>y;
cout<<fixed<<setprecision(4)<<j();
return 0;
}
而这个可以。
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double y;
double f_(double x){return 3*x*x;}
double f(double x){return x*x*x-y;}
double j(){
double x=y/3;
for(int i=1;i<=70;i++) x=x-f(x)/f_(x);
return x;
}
int main(){
cin>>y;
cout<<fixed<<setprecision(4)<<j();
return 0;
}