SGU - 123
Description Here is your second problem, keep calm and solve it . Nacci sequence of numbers is known to all : F1 = 1; F2 = 1; Fn+1 = Fn + Fn-1, for n>1. You have to find S - the sum of the first K Nacci numbers.
Input First line contains natural number K (0<K<41).
Output First line should contain number S.
Sample Input 5
Sample Output 12 Source |
简单数学求和题,,居然int都不会爆,,刚开始我还以为要long long。。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
int a[42] = {0,1,1};
int main()
{
int k;
for(int i=3; i<41; i++)
{
a[i] = a[i-1] + a[i-2];
}
scanf("%d", &k);
int ans = 0;
for(int i=1; i<=k; i++)
{
ans += a[i];
}
printf("%d\n", ans);
return 0;
}