题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
程序分析:请抓住分子与分母的变化规律。
递归解决:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <stdio.h> float Cal(float a,float b,int count) { if (count<=0) { return 0; }else { float temp = b/a; float c = a + b; return (temp + Cal(b, c, count-1)); } } int main( { float a = 1; float b = 2; int count = 20; float ret = Cal(a,b,count); printf("%lf",ret); return 0; }
|