C++ 用函数 迭代法 求x=根号a
#include<iostream.h>#include<math.h>doublediedai(doublem,doublea);staticdoublen;voidm...
#include<iostream.h>
#include<math.h>
double diedai(double m,double a);
static double n;
void main()
{
double a;
cin>>a;
cout<<diedai(a,a)<<endl;
}
double diedai(double m,double a)
{
n=1/2*(m+a/m);
if(fabs(m-n)>1e-7)
diedai(n,a);
return n;
}
哪儿错了 展开
#include<math.h>
double diedai(double m,double a);
static double n;
void main()
{
double a;
cin>>a;
cout<<diedai(a,a)<<endl;
}
double diedai(double m,double a)
{
n=1/2*(m+a/m);
if(fabs(m-n)>1e-7)
diedai(n,a);
return n;
}
哪儿错了 展开
2个回答
展开全部
#include<iostream.h>
#include<math.h>
double diedai(double m,double a);
static double n;
void main()
{
double a;
cin>>a;
cout<<diedai(a,a)<<endl;
}
double diedai(double m,double a)
{
n=1.0/2*(m+a/m); //1/2=0 1.0/2=0.5
if(fabs(m-n)>1e-7)
diedai(n,a);
return n;
}
展开全部
#include "stdafx.h"
#include "iostream"
#include "cmath"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const double eps=1e-7;
double m,n,a;
cout<<"请输入:";
cin>>a;
n=a;
while (1)
{
m=(n+a/n)/2;
if (fabs(m-n)<eps)
break;
n=m;
}
cout<<"绝对值为:"<<m<<endl;
return 0;
}
#include "iostream"
#include "cmath"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const double eps=1e-7;
double m,n,a;
cout<<"请输入:";
cin>>a;
n=a;
while (1)
{
m=(n+a/n)/2;
if (fabs(m-n)<eps)
break;
n=m;
}
cout<<"绝对值为:"<<m<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询