题目:

代码:
#include<stdio.h>
#include<math.h>
#define float double
float CubeRoot(int num){
float x = num;
float error = 1e-6;//float的话只能5位,6的化会死循环
while (fabs(num - (x * x * x)) >= error) {
x = (2 * x + num / (x * x)) / 3.0;
printf("%f\n",x);
}
return x;
}
int main(){
int temp;scanf("%d", &temp);
printf("%.2f",CubeRoot(temp));
return 0;
}
输出结果:

(题目的文字版:)
用牛顿迭代公式求 x 的立方根, 保留两位小数。计算公式为:

, 当

(ε为一个很小的数, 比如0.000001)时,

即为 x 的立方根。(注意:1/3的结果为0,写成1.0/3 可避免整除问题;使用fabs库函数求绝对值时需要include<math.h>。)