编写函数求出以下分数序列的前n项之和。和值作为函数值返回。 2/1,3/2,5/3,8/5,13/8,21/13,……
main()
{
float qh(int);
float sum;
int n;
scanf("%d",&n);
sum=qh(n);
printf("%f",sum);
getch();
}
float qh(int n)
{
float a, b, c, d, e, f,sum=0;
int i;
a = 2;b = 1;
c = 3;d = 2;
sum = a / b + c / d;
e = a + c;
f = b + d;
for(i=2;i<n;i++)
{
sum = sum + e / f;
e = e + c;
f = f + d;
c = e;
d = f;
}
return sum;
}