帮忙编个程序,c⼀c++的,很简单啊

2024-11-24 13:44:09
推荐回答(1个)
回答1:

#include
#include
#include

/* 计算a的三次方根,精度为precision.
*/
double san_ci_fang (double a, double precision) {
double x0 = 1;
double x1 = 1;
int k = 0;
while (k <= 100000) {
assert (x0 != 0);
x1 = 2*x0/3 + a/(3*x0*x0);
if (fabs(x1-x0) < precision)
return x1;
x0 = x1;
k++;
}
printf ("wrong, %.6lf\n", x1);
getch ();
exit(1);
return 0;
}

main () {
double a, b;
double precision = 10e-5;
scanf ("%lf", &a);
b = san_ci_fang (a, precision);
printf ("%.6lf^3 == %lf\n", b, pow(b, 3));
getch();
}