[UCSD白板题 ]Small Fibonacci Number
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的更多相关文章
- [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_ ...
- [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_ ...
- [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 ...
- 【LeetCode每天一题】Fibonacci Number(斐波那契数列)
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [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 ...
- [UCSD白板题] Pairwise Distinct Summands
Problem Introduction This is an example of a problem where a subproblem of the corresponding greedy ...
随机推荐
- redis实现tomcat集群session共享
1.部署两个tomcat节点,使用nginx实现集群(见http://www.cnblogs.com/zhangzhi0556/articles/nginx.html): 2.redis安装(见h ...
- Python中字符串操作
#Python字符串操作 '''1.复制字符串''' #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' pri ...
- Postgresql死锁处理
今天遇到Postgresql的一个问题,部分表记录的update一直无效报错,初步判断为锁表,赶紧进行解决. 1. 查询死锁进程列表 select * from pg_stat_activity wh ...
- MY SQL8.0里程碑发布
MySQL 开发团队于 12 日宣布 MySQL 8.0.0 开发里程碑版本(DMR)发布! 可能有人会惊奇 MySQL 为何从 5.x 一下跳跃到了 8.0.事实上,MySQL 5.x 系列已经延续 ...
- 解决 VMWARE MAC 10.12无法全屏的问题
昨天我在VMware上装了10.10,然后通过APP store 升级了系统到 10.12,升级前安装VMware tools 能自动全屏,可是升级后不行. 然后在网上查了很多资料,发现并没有这方面的 ...
- jQuery Mobile 网格布局
jQuery Mobile 布局网格 jQuery Mobile 提供了一套基于 CSS 的列布局方案.不过,一般不推荐在移动设备上使用列布局,这是由于移动设备的屏幕宽度所限. 但是有时你需要定位更小 ...
- 托管项目到github
将项目托管到github上面其实很简单,主要有以下几个步骤: 1.注册github账号 2.创建一个新的respository:命名这个respository(假设名字为Test),选择权限 3.创建 ...
- git 查看远程分支、本地分支、创建分支、把分支推到远程repository、删除本地分支
1 查看远程分支 $ git branch -a * br-2.1.2.2 master remotes/origin/HEAD -> origin/master remotes/origin/ ...
- Yii2 return redirect()
理想情况下是: return $this->redirect($url);立马跳转, 而不执行后续代码; 但是在init方法中是无效的,需要加上Yii::$app->end();即可终止后 ...
- ASI 与 AFN
HTTP终结者.功能十分强大. 基于底层的CFNetwork框架,运行效率很高. 可惜