SGU - 123 - The sum (简单数学!)

来源:互联网 发布:淘宝数字油画 编辑:程序博客网 时间:2024/05/18 02:00

SGU - 123

The sum
Time Limit: 250MS Memory Limit: 4096KB 64bit IO Format: %I64d & %I64u

Submit Status

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 longusing 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;} 


1 0
原创粉丝点击