标签:字符串
计算一个数字的立方根,不使用库函数
详细描述:
接口说明
原型:
public static double getCubeRoot(double input)
输入:double 待求解参数
返回值:double 输入参数的立方根
待求解参数 double类型
输入参数的立方根 也是double类型
216
6.0
import
java.util.Scanner;
public
class
Main {
public
static
void
main(String[] args) {
Scanner cin =
new
Scanner(System.in);
while
(cin.hasNext()) {
double
input = cin.nextDouble();
double
min =
0
;
double
max = input;
while
(max - min >
0.00001
) {
double
temp = (min + max) /
2
;
if
(temp*temp*temp > input) {
max = temp;
}
else
{
min = temp;
}
}
min*=
10
;
double
small = min - (
int
)min;
if
(small >=
0.5
) {
min++;
}
int
n = (
int
)min;
min=(
double
)n/
10
;
System.out.println(min);
}
}
}
//牛顿迭代
#include <stdio.h>
//#include <stdlib.h>
#define jingdu 0.0001
double newton_diedai( double a){
double xn,xn_1;
xn=1;
xn_1 = xn-((xn*xn*xn-a)/(3*xn*xn));
while (xn-xn_1>jingdu || xn-xn_1<-jingdu){
xn = xn_1;
xn_1 = xn-((xn*xn*xn-a)/(3*xn*xn));
}
return xn_1;
}
int main( void ){
double a;
scanf("%lf" , &a);
a = newton_diedai(a);
printf("%.1f" , a);
// system("pause" );
return 0;
}
本文出自 “走得远.看的深” 博客,谢绝转载!
标签:字符串
原文地址:http://2462852817.blog.51cto.com/11224080/1813910