zoukankan      html  css  js  c++  java
  • 7、斐波那契数列

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

    n<=39

    ===================Python================

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

    ======================Java=====================

    public class Solution {
        public int Fibonacci(int n) {
            int a = 0;
            int b = 1;
            if (n == 0) return 0;
            for (int i = 1; i <= n; i++){
                int tmp = a;
                a = b;
                b = tmp + b;
            }
            return a;
        }
    }
  • 相关阅读:
    页码数求0到9共有多少个
    reduce
    map,filter
    匿名函数 lambda
    递归
    python 切片
    函数
    集合(set)
    python 中random 库的使用
    printf 输出?
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13538071.html
Copyright © 2011-2022 走看看