斐波那契? (sdut oj)

来源:互联网 发布:淘宝店开什么好 编辑:程序博客网 时间:2024/06/06 08:28


斐波那契?

Time Limit: 1000MS Memory Limit: 32768KB


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


0 0
原创粉丝点击