有一分数序列:1/2-2/3+3/5-5/8+8/13-13/21...,求出这个数列的前20项之和用c语言编写
4个回答
展开全部
#include<stdio.h>
int main()
{
int i, numerator = 1, denominator = 2, t; //numerator为分子,denominator为分母
double sum;
for(i = 0, sum = 0; i < 20; i++)
{
sum = sum + (double)numerator/denominator;
//更高效的方法是 sum += (double)numerator/denominator;
t = numerator;
numerator = -denominator ; //后一项分子为前一项分母的相反数
denominator = denominator + t; //后一项分母为前一项分子分母之和
//更高效的方法是 denominator += t;
}
printf("%f",sum);
return 0;
}
int main()
{
int i, numerator = 1, denominator = 2, t; //numerator为分子,denominator为分母
double sum;
for(i = 0, sum = 0; i < 20; i++)
{
sum = sum + (double)numerator/denominator;
//更高效的方法是 sum += (double)numerator/denominator;
t = numerator;
numerator = -denominator ; //后一项分子为前一项分母的相反数
denominator = denominator + t; //后一项分母为前一项分子分母之和
//更高效的方法是 denominator += t;
}
printf("%f",sum);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
int main()
{
int i,j,k;
double sign = 1;
double sum = 0;
i = 1;
j = 2;
for(k=0;k<20;k++)
{
sum += i*sign/j;
sign *= -1;
j = i+j;
i = j-i;
}
printf("%lf\n",sum);
}
int main()
{
int i,j,k;
double sign = 1;
double sum = 0;
i = 1;
j = 2;
for(k=0;k<20;k++)
{
sum += i*sign/j;
sign *= -1;
j = i+j;
i = j-i;
}
printf("%lf\n",sum);
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int main()
{
int i,j,k;
double sign = 1;
double sum = 0;
i = 1;
j = 2;
for(k=0;k<20;k++)
{
sum += i*sign/j;
sign *= -1;
j = i+j;
i = j-i;
}
printf("%lf\n",sum);
}
{
int i,j,k;
double sign = 1;
double sum = 0;
i = 1;
j = 2;
for(k=0;k<20;k++)
{
sum += i*sign/j;
sign *= -1;
j = i+j;
i = j-i;
}
printf("%lf\n",sum);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询