Problem Introduction

The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_{i-1}+F_{i-2}\) for $ i \geq 2$.

Problem Description

Task.Given an integer \(n\),find the \(n\)th Fibonacci number \(F_n\).

Input Format.The input consists of a single integer \(n\).

Constraints.\(0 \leq n \leq 45\).

Output Format.Output \(F_n\).

Sample 1.
Input:

3

Output

2

Sample 2.
Input:

10

Output:

55

Solution

# Uses python3
def calc_fib(n):
    x,y = 0,1
    while n > 0:
        x,y,n = y,x+y,n-1
    return x
n = int(input())
print(calc_fib(n))

[UCSD白板题 ]Small Fibonacci Number的更多相关文章

  1. [UCSD白板题] Huge Fibonacci Number modulo m

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  2. [UCSD白板题] The Last Digit of a Large Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  3. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  4. 【LeetCode每天一题】Fibonacci Number(斐波那契数列)

    The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...

  5. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  6. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  7. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  8. [UCSD白板题] Points and Segments

    Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...

  9. [UCSD白板题] Pairwise Distinct Summands

    Problem Introduction This is an example of a problem where a subproblem of the corresponding greedy ...

随机推荐

  1. R常见的几种常见统计图

    1,向日葵散点图 2,热图  (颜色越深,数值越大) 3,折线图(散点图),绘制散点图集用 paris(data.frame)

  2. jQuery插件制作方法

    html页面:<h1>Hello My name is Alex,i from china.</h1> 1.无参数实现文字阴影效果 测试代码: $("h1" ...

  3. 完整地mybatis + springmvc用checkbox实现批量删除

    因为自己在网上找了半天,都找不到完整地代码(脑袋笨,不会变通到自己项目里),所以在这里记下了近乎完整的代码 前端代码 <span style="cursor:pointer;" ...

  4. android之handle

    Android中异步消息处理主要由四个部分组成,Message.handler.messageQueue和looper. 1.message message是线程之间传递的消息,他可以在内部携带少量的 ...

  5. c++模板使用出错情况error LNK2019: unresolved external symbol "public: float __thiscall Compare<float>::min(void)" (?min@?$Compare@M@@QAEMXZ) referenced in function _main

    将类模板在头文件中定义,类的成员函数在头文件中声明,头文件中只留下接口,函数的实现在另一个.cpp文件中,这样编译出来错误error LNK2019: unresolved external symb ...

  6. ajaxSubmit

    $('button').on('click', function() {    $('form').on('submit', function() {        var title = $('in ...

  7. aspx页面,中文乱码解决方案

    由于文件编码方式编码方式不统一出现样式中文乱码解决方案: 今天碰到的问题:页面字体样式设置的'微软雅黑',可页面没引用.我调试看到样式出现中文乱码了 这种问题,就需要转换文件的编码方式,如下两步即可解 ...

  8. generator class 的含义

    native有天生的,本土的,也就是说生来就有的, 那也就是说自动生成,不需要人工来帮忙或者管控的. 而assigned是指指定的,分配的, 如果你不赋予他甚麼东西,那麼他是不能实现的. 需要人工,自 ...

  9. 去空格 whitespaceAndNewlineCharacterSet和过滤字符串

    一.过滤字符串 可以使用stringByTrimmingCharactersInSet函数过滤字符串中的特殊符号 首先自己定义一个NSCharacterSet, 包含需要去除的特殊符号 NSChara ...

  10. C# 委托如何理解 打个比喻

    初学者可能会给winform窗体注册事件,也听过事件是基于委托实现的 那么,委托是什么,事件又是什么,委托和事件是什么关系. 个人喜欢做一些比喻,把这些东西想象成某一个模型,这样方便记忆,理解,随着对 ...