1173 - C语言6.21

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

用牛顿迭代法求以下方程在1.5附近的根:

题目输入

题目输出

输出牛顿迭代法求出的根,保留4位小数。

请注意行尾输出换行。

输入/输出样例

输入格式

输出格式

2.0000

C语言解答

#include <stdio.h>
#include <math.h>
int main() {
        float x;
        x = 1.5;
        while (fabs(2 * x * x * x - 4 * x * x + 3 * x - 6) > 1e-6) {
                x = x - (2 * x * x * x - 4 * x * x + 3 * x - 6) / (6 * x * x - 8 * x + 3);
        }
        printf("%.4f\n", x);
        return 0;
}

C++解答

#include <stdio.h>
#include <math.h>
int main() {
	float x;
	x = 1.5;
	while (fabs(2 * x * x * x - 4 * x * x + 3 * x - 6) > 1e-6) {
		x = x - (2 * x * x * x - 4 * x * x + 3 * x - 6) / (6 * x * x - 8 * x + 3);
	}
	printf("%.4f\n", x);
	return 0;
}

Java解答


public class Main {
	public static void main(String[] args) {
		double f,fdao,x0,x;
		x = 1.5;
		do
		{
			x0 = x;
			f = 2 * x0 * x0 * x0  - 4*x0*x0+3*x0-6;
			fdao = 6*x0*x0 -8*x0+3;
			x = x0 - f/fdao;
			
		}while ( Math.abs(x-x0)>=1e-5 ) ;
		System.out.printf("%.4f\n",x);
	}

}