zoukankan      html  css  js  c++  java
  • 剑指07斐波那契数列

    题目描述

    大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0,第1项是1)。

    n<=39

    class Solution {
    public:
        int Fibonacci(int n) {
            int f=0,g=1;
            while (n--){
                g+=f;
                f=g-f;
            }
            return f;

        }
    };

    public class Solution {
        public int Fibonacci(int n) {
         int target=0;
            if (n==0)
                return 0;
            if (n==1)
                return 1;
            int one=0,two=1;
            for (int i=2;i<=n;i++){
                target=one+two;
                one=two;
                two=target;
            }
            return target;
        }
    }

    # -*- coding:utf-8 -*-
    class Solution:
        def Fibonacci(self, n):
            # write code here
            f1=0
            f2=1
            for i in range(n):
                f1,f2=f2,f1+f2
            return f1
       

  • 相关阅读:
    输入和输出

    4. 深入 Python 流程控制
    js下拉框选择图片
    按下enter触发事件
    js多种方法取数组的最后一个元素
    apply,call,bind函数作用与用法
    vue中的js绑定样式
    js添加删除class
    rem等比例自适应手机尺寸
  • 原文地址:https://www.cnblogs.com/hrnn/p/13359009.html
Copyright © 2011-2022 走看看