6个回答
展开全部
给你一个函数的算法,改写成一个函数就可以啦,nNum就是前n项和
int nNum = 20;
double dSum = 0;
double nLast = 0;
double nCur = 1;
double nFuture = 1;
for (int i = 1; i <= nNum; i++)
{
double dFenzi = nCur + nFuture;
double dFenmuu = nLast + nCur;
dSum += dFenzi / dFenmuu;
nLast = nCur;
nCur = nFuture;
nFuture = nLast + nCur;
}
函数
double GetSum(int &nNum)
{
double dSum = 0;
double nLast = 0;
double nCur = 1;
double nFuture = 1;
for (int i = 1; i <= nNum; i++)
{
double dFenzi = nCur + nFuture;
double dFenmuu = nLast + nCur;
dSum += dFenzi / dFenmuu;
nLast = nCur;
nCur = nFuture;
nFuture = nLast + nCur;
}
}
调用
GetSum(20);
int nNum = 20;
double dSum = 0;
double nLast = 0;
double nCur = 1;
double nFuture = 1;
for (int i = 1; i <= nNum; i++)
{
double dFenzi = nCur + nFuture;
double dFenmuu = nLast + nCur;
dSum += dFenzi / dFenmuu;
nLast = nCur;
nCur = nFuture;
nFuture = nLast + nCur;
}
函数
double GetSum(int &nNum)
{
double dSum = 0;
double nLast = 0;
double nCur = 1;
double nFuture = 1;
for (int i = 1; i <= nNum; i++)
{
double dFenzi = nCur + nFuture;
double dFenmuu = nLast + nCur;
dSum += dFenzi / dFenmuu;
nLast = nCur;
nCur = nFuture;
nFuture = nLast + nCur;
}
}
调用
GetSum(20);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
main()
{
int n;
double a=1,b=2,m=0;
for(n=1;n<21;n++)
{
m=m+b/a;
b=a+b;
a=b-a;
}
printf("%lf",m);
}
{
int n;
double a=1,b=2,m=0;
for(n=1;n<21;n++)
{
m=m+b/a;
b=a+b;
a=b-a;
}
printf("%lf",m);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int i;
double a=2, b;
double sum = a;
for (i=0; i<20; ++i) {
b = 1+1/a;
a = b;
sum += b;
}
printf("%f\n",sum);
double a=2, b;
double sum = a;
for (i=0; i<20; ++i) {
b = 1+1/a;
a = b;
sum += b;
}
printf("%f\n",sum);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
找出数学规律
an-1=b/a
an=(a+b)/b
至于编程就交给你了 也不是很难的
an-1=b/a
an=(a+b)/b
至于编程就交给你了 也不是很难的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询