• 18. 爱吃皮蛋的小明(斐波那契数列)


    时间限制: 1 s

     空间限制: 32000 KB

     题目等级 : 白银 Silver

    题解

    题目描述 Description

    小明特别爱吃蛋,特别是皮蛋。他一次可以吃一个蛋或者两个蛋(整个吞下去),而且他喜欢吃得有花样,他想知道对于一定蛋的数量,有几种不同的吃法。

    输入描述 Input Description

    一行一个整数N,表示皮蛋的数量

    输出描述 Output Description

    一行一个整数sum,表示吃法总数

    样例输入 Sample Input

    3

    样例输出 Sample Output

    3

     

    说明:有以下3种吃法

    1+1+1

    1+2

    2+1

    数据范围及提示 Data Size & Hint

    0

    解析:递推算法f(n)=f(n-1)+f(n-2),注意数据特别大时,用longlongprintf输出考试用“%lld,,考试系统认“%lld”,windouw系统认“%I64d”。

    代码:

    #include

    using namespace std;

    #include

    long long f[100];

    int main()

    {

           int n;

           scanf("%d",&n);

           f[1]=1;f[2]=2;

           for(int i=3;i<=n;++i)

           f[i]=f[i-1]+f[i-2];

           printf("%lld",f[n]);

           return 0;

     }

  • 相关阅读:
    HTTP协议简介
    Web开发中B/S架构和C/S架构的区别
    软件测试作业三
    Java8 时间处理
    Java EE
    Java 中的 I/O 抽象
    Python 高级 I/O 多路复用
    SQL 与关系代数
    Python 协程与事件循环
    Java SE 5.0
  • 原文地址:https://www.cnblogs.com/csgc0131123/p/5290525.html
Copyright © 2020-2023  润新知