斐波那契数列

10:斐波那切数列

对着背影说爱祢 提交于 2019-12-02 14:59:34
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。n<=39 n=0时,f(n)=0 n=1时,f(n)=1 n>1时,f(n)=f(n-1)+f(n-2) 法一:用循环较快 class Solution : def fib ( self , x ) : if x <= 0 : return 0 elif x == 1 : return 1 else : n , a , b = 0 , 0 , 1 while n + 1 != x : a , b = b , a + b n += 1 return b a = Solution ( ) b = a . fib ( 4 ) print ( b ) 法二:用递归,较慢 class Solution : def fib ( self , x ) : if x <= 0 : return 0 elif x == 1 : return 1 else : return self . fib ( x - 1 ) + self . fib ( x - 2 ) a = Solution ( ) b = a . fib ( 4 ) print ( b ) 来源: https://blog.csdn.net/qq_43275748/article/details/102778053

剑指offer 斐波那契数列

孤者浪人 提交于 2019-12-02 12:32:01
题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。 n<=39 解法: public class Solution { public int Fibonacci ( int n ) { int pre = 0 ; int cur = 1 ; if ( n <= 1 ) return n ; while ( n - 1 > 0 ) { cur = pre + cur ; pre = cur - pre ; n -- ; } return cur ; } } 题目详解参考我的另一篇文章 leetcode刷题 斐波那契数列 来源: https://blog.csdn.net/weixin_43105156/article/details/102760840

斐波那契数列的递归递推求法

江枫思渺然 提交于 2019-12-02 11:46:32
# include <bits/stdc++.h> using namespace std ; // 递归计算斐波那契数列 int f1 ( int n ) { if ( n == 1 || n == 2 ) return 1 ; return f1 ( n - 1 ) + f1 ( n - 2 ) ; } // 递推计算斐波那契数列 int f2 ( int n ) { if ( n == 1 || n == 2 ) return 1 ; int fa = 1 , fb = 1 ; for ( int i = 3 ; i <= n ; ++ i ) { int t = fa + fb ; fa = fb ; fb = t ; } return fb ; } int main ( ) { int n ; scanf ( "%d" , & n ) ; int ans1 = 0 , ans2 = 0 ; ans1 = f1 ( n ) ; ans2 = f2 ( n ) ; printf ( "递归结果:%d\n递推结果:%d" , ans1 , ans2 ) ; } 来源: https://blog.csdn.net/qq_40432713/article/details/102757682

斐波那契数列

偶尔善良 提交于 2019-12-02 11:23:35
问题描述: 一列数:从1开始,前两项为1,从第三项开始每一项等于前两项之和。 例:1 1 2 3 5 8 13 推导出公式则为: n<3时 f(n)=1; n>2时 f(n)=f(n-1)+f(n-2) 实现代码如下: # include <stdio.h> int Fibon ( int n ) { int f1 = 1 ; int f2 = 1 ; int f3 = 1 ; for ( int i = 2 ; i < n ; i ++ ) { f3 = f1 + f2 ; f1 = f2 ; f2 = f3 ; } return f3 ; } int main ( ) { for ( int i = 1 ; i < 10 ; i ++ ) { printf ( "%d\n" , Fibon ( i ) ) ; } return 0 ; } 来源: https://blog.csdn.net/weixin_44801392/article/details/102755635

2019E1_F 斐波那契数列

依然范特西╮ 提交于 2019-12-02 10:30:14
F 斐波那契数列 题目 相信大家都学过斐波那契数列,虽然很简单,但是斐波那契数列却是很重要的哦,那么让我们来复习一下斐波那契数列吧! 输入 多组数据输入 每行一个整数n(0<n≤30) 输出 对于每组数据,输出一行,为斐波那契数列第n项的值 输入样例 1 2 3 4 输出样例 1 1 2 3 思路 斐波那契数列 代码 # include <cstdio> # include <iostream> using namespace std ; long long a [ 40 ] ; int main ( ) { int n ; a [ 1 ] = 1 ; a [ 2 ] = 1 ; for ( int i = 3 ; i < 39 ; i ++ ) a [ i ] = a [ i - 1 ] + a [ i - 2 ] ; while ( cin >> n ) { printf ( "%lld\n" , a [ n ] ) ; } return 0 ; } 来源: https://blog.csdn.net/weixin_44024733/article/details/102752512

PHP中如何实现斐波那契数列?

拥有回忆 提交于 2019-12-02 03:28:12
在本文中,我们将给大家介绍如何用PHP实现斐波那契数列。给定一个数字n,我们需要找到斐波那契数列直到第n项。 例子: 输入:10 输出:0 1 1 2 3 5 8 13 21 34 输入:15 输出:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 方法1:使用递归方式 递归是我们重复调用相同函数直到匹配基本条件以结束递归的方式。 <?php function Fibonacci($number){ if ($number == 0) return 0; else if ($number == 1) return 1; else return (Fibonacci($number-1) + Fibonacci($number-2)); } $number = 10; for ($counter = 0; $counter < $number; $counter++){ echo Fibonacci($counter),' '; } 输出: 10 1 1 2 3 5 8 13 21 34 方法2:使用迭代方法 首先,我们将第一个和第二个数字初始化为0和1.然后,我们打印第一个和第二个数字。然后我们将流程发送到迭代while循环,我们通过添加前两个数字得到下一个数字,同时我们将第一个数字与第二个数字交换,第二个数字与第三个数字交换。 <?php

剑指offer#10斐波那契数列 && #10-2跳台阶 &&10-3跳台阶进阶问题 &&矩形覆盖

和自甴很熟 提交于 2019-12-02 02:36:36
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。n<=39。 分析:使用递归会有很多重复计算,因此利用从下往上的思想,用循环来改进 class Solution { public: int Fibonacci(int n) { int result[2] = {0,1}; if(n<2)return result[n]; int first=0,second=1,i=2,third ; while(i<=n){ third = first + second; first = second; second = third; i++; } return third; } }; **复杂度分析: 时间复杂度:O(n) 空间复杂度:O(1) ** 跳台阶:一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。 分析:假设n级台阶的跳法是f(n),那么最后一步可以是跳一个台阶,也可以是跳两个台阶,那么这样子的话f(n)由两种方式组成,一种是f(n-1)然后最后一步跳一个台阶,另一种是f(n-2)个台阶的跳法最后跳两个台阶.因此这其实也是斐波那契数列. 解法: class Solution { public: int jumpFloor(int number) { /

斐波那契数列的实现(四种方法 递归,非递归)

别来无恙 提交于 2019-12-01 20:53:00
要实现斐波那契数列,首先先要了解什么是斐波那契数列 0 ,1,1,2,3,5,8,13...这样的数列称为斐波那契数列 一 非递归 int Fib(int n) { if (n == 1) { return 0; } if (n == 2) { return 1; } int f1 = 0; int f2 = 1; int c = 0; for (int i = 3; i <= n; i++) { c = f1 + f2; f1 = f2; f2 = c; } return c; } int main() { int n; scanf("%d", &n); int ret=Fib(n); printf("%d", ret); system("pause"); return 0; } 二 非递归 int Fib(int n) { int f1 = 0; int f2 = 1; if (n >= 1) { return n - 1; } for (int i = 1; i <= n / 2; i++) { f1 = f1 + f2; f2 = f1 + f2; } if (n / 2 != 0) { return f1; } else { return f2; } } 三 递归 int Fib(int n)//递归 { if (n == 1) { return 0; } if (n =