站外题求助
  • 板块学术版
  • 楼主wmzwk
  • 当前回复3
  • 已保存回复3
  • 发布时间2025/7/30 09:06
  • 上次更新2025/7/30 13:57:01
查看原帖
站外题求助
1273755
wmzwk楼主2025/7/30 09:06

题目描述

给定一个实数 yy,求 yy 的三次方根。

为什么这个不行(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;
}
2025/7/30 09:06
加载中...