该题目的类型又是利用程序求解数学问题,难度水平:低
核心思想:二分搜索
代码如下所示
public class JieQuestion {
public static void main(String[] args) {
double middlepoint;
double startpoint = 0;
double endpoint = 3;
double temp;
do {
middlepoint = (startpoint + endpoint) / 2;
temp = middlepoint * middlepoint * middlepoint - middlepoint * middlepoint - 1;
if (temp > 0) {
if (abs(temp) < 10E-4) {
System.out.println(temp);
} else {
endpoint = middlepoint;
}
} else {
if (abs(temp) < 10E-4) {
System.out.println(temp);
} else {
startpoint = middlepoint;
}
}
} while (abs(temp) >10E-4);
}
static double abs(double x) {
return x > 0 ? x : -x;
}
}
运行结果
通过本题目,我加深了对二分搜索思想的理解,同时熟练了Do-while 循环的使用方法,同时也对这种题目(用代码表示数学问题)有了更深的把握