北航的这道考研上机真题应该是考的我们的《数值分析》基本功。记得原来上《数值分析》课的时候学习过平方根的迭代公式,好像还有有通用的求根公式,不记得了。这种类型的迭代公式其实很简单。只要一个变量便够了,然后不断的修改。
题目URL:http://ac.jobdu.com/problem.php?id=1166
我的AC代码,欢迎拍砖。
#include<iostream>
#include<stdio.h>
using namespace std;
double iterate(double y, double x)
{
return y * 2 / 3.0 + x / (3 * y * y);
}
int main()
{
double x, y;
int n;
while(scanf("%lf%d", &x, &n) != EOF)
{
y = x;
for(int i=0; i<n; i++)
{
y = iterate(y, x);
}
printf("%.6lf\n", y);
}
system("pause");
return 0;
}