M29917: 牛顿迭代法
implementation, http://cs101.openjudge.cn/practice/29917/
cpp
#include<bits/stdc++.h>
using namespace std;
double const c=1e-6;
double a,x1,x2;
int cnt;
int main(){
while(scanf("%lf",&a)!=EOF){
cnt=0;
x1=1;
while(1){
cnt++;
x2=x1-(x1*x1-a)/(2*x1);
if(abs(x1-x2)<=c)
break;
x1=x2;
}
printf("%d %.2lf\n",cnt,x2);
}
return 0;
}