题目
题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和。
实现
- C++
#include <iostream>
using namespace std;
int main(){
float a=2.0,b=1.0,sum=0;
for(int i=0;i<20;i++){
sum+=a/b;
float temp=a;
a=a+b;
b=temp;
}
printf("%f",sum);
return 0;
}
- python
a=2.0
b=1.0
s=0
for n in range(1,21):
s+=a/b
tmp=a
a=a+b
b=tmp
print(s)