sgu 123 The sum

本文详细介绍了如何通过预处理或直接暴力求解的方法来计算Fibonacci数列的前K项之和。通过代码实现展示了具体的步骤和过程,适合初学者理解和掌握。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

123. The sum

time limit per test: 0.5 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


告诉你K,求前K个Fibonacci数的和。预处理一下或直接暴搞就可以,属于水题。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))

using namespace std;
int const nMax = 50;
typedef long long LL;
typedef pair<LL,LL> pij;

LL Fi[nMax];
LL sum[nMax];

int main(){
    Fi[0]=0,Fi[1]=1;
    for(int i=2;i<nMax;i++)Fi[i]=Fi[i-1]+Fi[i-2];
    sum[0]=0;
    for(int i=1;i<nMax;i++){
        sum[i]=sum[i-1]+Fi[i];
    }
    int n;
    while(cin>>n){
        cout<<sum[n]<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值