习题 3.21 有一分数列2/1, 3/2, 5/3, 8/5, 13/8, 21/13, ···求出这个数列的前20项之和。
代码:
#include<iostream>
using namespace std;
int main()
{
int i,j;
float t1,t2,t,sum;
for (i = 0,sum = 0.0,t1 = 1.0,t2 = 2.0; i <= 20; ++i) //t2为分子,t1为分母
{
t = t2;
t2 = t1 + t2;
t1 = t;
sum += (t2 / t1);
}
cout << "前20项之和为:" << sum << endl;
return 0;
}
运行结果: