123. The sum
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
The Fibonacci sequence of numbers is known: F1 = 1; F2 = 1; Fn+1 = Fn + Fn-1, for n>1. You have to find S - the sum of the first K Fibonacci numbers.
Input
First line contains natural number K (0<K<41).
Output
First line should contain number S.
Sample Input
5
Sample Output
12
计算斐波那契数列前N项和,水了。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
long long x[45];
void solve(){
x[1]=x[2]=1;
for(int i=3;i<=41;i++){
x[i]=x[i-1]+x[i-2];
}
int n;
scanf("%d",&n);
long long ans=0;
for(int i=1;i<=n;i++){
ans+=x[i];
}
printf("%lld",ans);
}
int main() {
#ifdef LOCAL_DEFINE
freopen("input.txt", "rt", stdin);
#endif
solve();
return 0;
}