斐波那契?

来源:互联网 发布:淘宝儿童折叠床 编辑:程序博客网 时间:2024/06/04 18:03

斐波那契?

Time Limit: 1000MS Memory Limit: 32768KB
SubmitStatistic

Problem Description

给出一个数列的递推公式,希望你能计算出该数列的第N个数。递推公式如下:

F(n)=F(n-1)+F(n-2)-F(n-3). 其中,F(1)=2, F(2)=3, F(3)=5.

很熟悉吧,可它貌似真的不是斐波那契数列呢,你能计算出来吗?

Input

   输入只有一个正整数N(N>=4).

Output

   输出只有一个整数F(N).

Example Input

5

Example Output

8

Hint


Author

#include <stdio.h>
#include <math.h>
int f(int n)
{
    int l;
    if(n==1)l=2;
    if(n==2)l=3;
    if(n==3)l=5;
    if(n>3)l=f(n-1)+f(n-2)-f(n-3);
    return l;
}
int main()
{
    int ans,n;
    scanf("%d",&n);
    ans=f(n);
    printf("%d\n",ans);
    return 0;
}

原创粉丝点击