当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

3727:Newton’s Method

题目描述

While solving non-linear equations in numerical analysis lesson, professor Busoniya introduce us Newton's Method. Like the other ways (such as bisection, Muller's Method), this method is also based on a linear approximation of the function, but does so using a tangent to the curve. The figure below gives a graphical description.


Starting from a single initial estimate, x0, which is not too far from a root, we move along the tangent to its intersection with the x-axis, and take that as the next approximation x1. The general term is: xn+1 = xn - f(xn) / f′(xn), n = 0, 1, 2, .... Newton’s algorithm is widely used because it is more rapidly convergent than any of the methods discussed so far. Then now, you task is to accurate how many iterations it takes to get a root using Newton's Method. The x0 and the equation are given and all the tolerance is 0.000001. You just use this method to get the answer. The iteration will stop when |xn+1 - xn| < tolerance or |f(xn)| < tolerance then n is the answer. But sometimes the x0 or equation we choose is so bad that we cannot get the answer using this method within 1000 iterations, then just output "Bad x0 or bad equation!"

输入解释

There are multiple cases ended by EOF. Each case has two lines, first line is the equation and the second line is x0. We ensure that each equation just contain integers, 'x', '+', '-', '=' and '^', the right part of '=' is always 0, and there is a space between each term. The number of terms in the equation is less than 50 and all integers in the equations are less than 1000.

输出解释

For each test case, output n or "Bad x0 or bad equation!" per line.

输入样例
-3x^2 -3 = 0
3
3x -3 = 0
1
5x^2 -2 = 0
-5
输出样例
Bad x0 or bad equation!
0
6

该题目是Virtual Judge题目,来自 北京大学POJ

源链接: POJ-3727

最后修改于 2020-10-29T07:09:13+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
1000 65536