斐波那契?

来源:互联网 发布:淘宝店卖虚拟物品 编辑:程序博客网 时间:2024/06/04 19:57

斐波那契?

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



代码:

#include<stdio.h>int main(){    int f(int n);    int n;    scanf("%d",&n);    printf("%d\n",f(n));    return 0;}int f(int n){    int c;    if(n == 1)    {        c = 2;    }    if(n == 2)    {        c = 3;    }    if(n == 3)    {        c = 5;    }    if(n > 3)    {        c = f(n - 1) + f(n - 2) - f(n - 3);    }    return c;}
原创粉丝点击