建民打卡日记4.18
一、问题描述

二、设计思路

三、程序流程图

四、代码实现
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double newt(float a, float b, float c, float d);
float a, b, c, d, x;
cin >> a >> b >> c >> d;
x = newt(a, b, c, d);
printf("%lf", x);
}
double newt(float a, float b, float c, float d) {
double x0, x = 1.5, f, fd, h;
double k;
while (x) {
x0 = x;
f = a * x0 * x0 * x0 + b * x0 * x0 + c * x0 + d;
fd = 3 * a * x0 * x0 + 2 * b * x0 + c;
h = f / fd;
x = x0 - h;
k = x - x0;
if (k < 0)
k *= -1;
if (k <= 1e-5)
break;
}
return x;
}


浙公网安备 33010602011771号