Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前 20 项之和。
- //递归算法
- // a分子 b分母
- #include <stdio.h>
- float calc(float a,float b,float n)
- {
- float tmp;
- static float s;
- s=s+a/b;
- tmp=b;
- b=a;
- a=a+tmp;
- if(n>1)
- return calc(a,b,n-1);
- else
- return s;
- }
- int main()
- {
- float x;
- x = calc(2, 1, 20);
- printf("x = %f\n", x);
- return 0;
- }
- // 修改数据类型
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement