21.求出数列2/1+3/2+5/3+8/5+13/8+。。。。的前20项之和

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     double s=0,a=1,b=2,t,i;
 7     for(i=1;i<=20;i++)
 8     {
 9         s = s+b/a;  //利用交换的思想,设置一个中间变量t,把分子换到分母上来,然后把分子分母之和换到分子上去
10         t = a;
11         a = b;
12         b = b+t;
13     }
14     printf("%15.6f\n",s);
15     return 0;
16 }

猜你喜欢

转载自www.cnblogs.com/spore/p/10370368.html