1833 - 课后习题6.8
有一分数序列:
2/1 3/2 5/3 8/5 13/8 21/13......
求出这个数列的前N项之和,保留两位小数。
Input
N
Output
数列前N项和
Examples
Input
10
Output
16.48
Solution C
#include<stdio.h> int main(void) { int x1,x2,x3,y1,y2,y3,N; scanf("%d",&N); if(N==1) printf("2\n"); else { if(N==2) printf("3.5\n"); else { int i; double s=3.5; x1=2; x2=3; y1=1; y2=2; for(i=3;i<=N;i++) { x3=x1+x2; y3=y1+y2; s+=x3*1.0/y3; x1=x2; x2=x3; y1=y2; y2=y3; } printf("%.2f\n",s); } } }
Solution C++
#include <bits/stdc++.h> using namespace std; long long n; float s,a=2,b=1,t; int main() { cin>>n; for(int i=1;i<=n;i++) s=s+1/b*a,t=b+a,b=a,a=t; printf("%.2f",s); return 0; }