- 【例30.1】 平方根
分享一下牛顿迭代法
- 2025-4-16 12:09:25 @
'''c++ #include #include #include #include using std::cin; using std::cout; using std::fixed; using std::setprecision;
double open_root(double num=2, double precision=1e-15) { double old_x=1; double new_x;
while (true)
{
new_x = old_x - (old_x * old_x - num) / (2 * old_x);
if (fabs(new_x - old_x) <= precision)
{
return new_x;
}
old_x = new_x;
}
}
int main() { double n; cin >> n; cout << fixed << setprecision(16) << open_root(n); return 0; }
'''
0 comments
No comments so far...
Information
- ID
- 91
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 10
- Tags
- # Submissions
- 6
- Accepted
- 3
- Uploaded By