Advertisement
Guest User

Untitled

a guest
Aug 5th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.47 KB | None | 0 0
  1. //题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前 20 项之和。
  2. //递归算法
  3. // a分子 b分母
  4. #include <stdio.h>
  5. float calc(float a,float b,float n)
  6. {
  7.     float tmp;
  8.     static float s;
  9.     s=s+a/b;
  10.     tmp=b;
  11.     b=a;
  12.     a=a+tmp;
  13.     if(n>1)
  14.         return calc(a,b,n-1);
  15.     else
  16.         return s;
  17.  
  18. }
  19.  
  20. int main()
  21. {
  22.     float x;
  23.  
  24.     x = calc(2, 1, 20);
  25.     printf("x = %f\n", x);
  26.     return 0;
  27. }
  28. // 修改数据类型
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement