剑指offer——面试题10:斐波那契数列

news/2024/11/10 0:12:11 标签: 面试, 数据结构与算法

个人答案:

 1 #include"iostream"
 2 #include"stdio.h"
 3 #include"string.h"
 4 using namespace std;
 5 typedef long long ll;
 6 const int MAXN=10000;
 7 
 8 ll fib[MAXN];
 9 ll Fibonacci(int n)
10 {
11     if(fib[n]!=-1)
12         return fib[n];
13     return fib[n]=Fibonacci(n-1)+Fibonacci(n-2);
14 }
15 
16 int main()
17 {
18     int n;
19     memset(fib,-1,sizeof(fib));
20     fib[0]=0;
21     fib[1]=1;
22     while(cin>>n)
23     {
24         cout<<Fibonacci(n)<<endl;
25     }
26     return 0;
27 }
View Code

官方答案:

  1 // 面试题10:斐波那契数列
  2 // 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项。
  3 
  4 #include <cstdio>
  5 
  6 // ====================方法1:递归====================
  7 long long Fibonacci_Solution1(unsigned int n)
  8 {
  9     if(n <= 0)
 10         return 0;
 11 
 12     if(n == 1)
 13         return 1;
 14 
 15     return Fibonacci_Solution1(n - 1) + Fibonacci_Solution1(n - 2);
 16 }
 17 
 18 // ====================方法2:循环====================
 19 long long Fibonacci_Solution2(unsigned n)
 20 {
 21     int result[2] = {0, 1};
 22     if(n < 2)
 23         return result[n];
 24 
 25     long long  fibNMinusOne = 1;
 26     long long  fibNMinusTwo = 0;
 27     long long  fibN = 0;
 28     for(unsigned int i = 2; i <= n; ++ i)
 29     {
 30         fibN = fibNMinusOne + fibNMinusTwo;
 31 
 32         fibNMinusTwo = fibNMinusOne;
 33         fibNMinusOne = fibN;
 34     }
 35 
 36      return fibN;
 37 }
 38 
 39 // ====================方法3:基于矩阵乘法====================
 40 #include <cassert>
 41 
 42 struct Matrix2By2
 43 {
 44     Matrix2By2
 45     (
 46         long long m00 = 0, 
 47         long long m01 = 0, 
 48         long long m10 = 0, 
 49         long long m11 = 0
 50     )
 51     :m_00(m00), m_01(m01), m_10(m10), m_11(m11) 
 52     {
 53     }
 54 
 55     long long m_00;
 56     long long m_01;
 57     long long m_10;
 58     long long m_11;
 59 };
 60 
 61 Matrix2By2 MatrixMultiply
 62 (
 63     const Matrix2By2& matrix1, 
 64     const Matrix2By2& matrix2
 65 )
 66 {
 67     return Matrix2By2(
 68         matrix1.m_00 * matrix2.m_00 + matrix1.m_01 * matrix2.m_10,
 69         matrix1.m_00 * matrix2.m_01 + matrix1.m_01 * matrix2.m_11,
 70         matrix1.m_10 * matrix2.m_00 + matrix1.m_11 * matrix2.m_10,
 71         matrix1.m_10 * matrix2.m_01 + matrix1.m_11 * matrix2.m_11);
 72 }
 73 
 74 Matrix2By2 MatrixPower(unsigned int n)
 75 {
 76     assert(n > 0);
 77 
 78     Matrix2By2 matrix;
 79     if(n == 1)
 80     {
 81         matrix = Matrix2By2(1, 1, 1, 0);
 82     }
 83     else if(n % 2 == 0)
 84     {
 85         matrix = MatrixPower(n / 2);
 86         matrix = MatrixMultiply(matrix, matrix);
 87     }
 88     else if(n % 2 == 1)
 89     {
 90         matrix = MatrixPower((n - 1) / 2);
 91         matrix = MatrixMultiply(matrix, matrix);
 92         matrix = MatrixMultiply(matrix, Matrix2By2(1, 1, 1, 0));
 93     }
 94 
 95     return matrix;
 96 }
 97 
 98 long long Fibonacci_Solution3(unsigned int n)
 99 {
100     int result[2] = {0, 1};
101     if(n < 2)
102         return result[n];
103 
104     Matrix2By2 PowerNMinus2 = MatrixPower(n - 1);
105     return PowerNMinus2.m_00;
106 }
107 
108 // ====================测试代码====================
109 void Test(int n, int expected)
110 {
111     if(Fibonacci_Solution1(n) == expected)
112         printf("Test for %d in solution1 passed.\n", n);
113     else
114         printf("Test for %d in solution1 failed.\n", n);
115 
116     if(Fibonacci_Solution2(n) == expected)
117         printf("Test for %d in solution2 passed.\n", n);
118     else
119         printf("Test for %d in solution2 failed.\n", n);
120 
121     if(Fibonacci_Solution3(n) == expected)
122         printf("Test for %d in solution3 passed.\n", n);
123     else
124         printf("Test for %d in solution3 failed.\n", n);
125 }
126 
127 int main(int argc, char* argv[])
128 {
129     Test(0, 0);
130     Test(1, 1);
131     Test(2, 1);
132     Test(3, 2);
133     Test(4, 3);
134     Test(5, 5);
135     Test(6, 8);
136     Test(7, 13);
137     Test(8, 21);
138     Test(9, 34);
139     Test(10, 55);
140 
141     Test(40, 102334155);
142 
143     return 0;
144 }
View Code

 

转载于:https://www.cnblogs.com/acm-jing/p/10389502.html


http://www.niftyadmin.cn/n/706646.html

相关文章

395. Longest Substring with At Least K Repeating Characters

题目要求 Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.Example 1:Input: s "aaabb", k 3Output: 3The longest substring is "aaa&qu…

Asp.Net实现JS前台带箭头的流程图方法总结!(个人笔记,信息不全)

Asp.Net实现JS前台带箭头的流程图方法总结&#xff01;&#xff08;持续更新中&#xff09; 一、返回前台json格式 json5 "[{\"Id\":2259,\"Name\":\"高中\"},{\"Id\":2259,\"tName\":\"初中\"},{"Id&…

Java 对象的继承,抽象类,接口

子父级继承 关键字 extends 首先创建一个父类 1 class Fu 2 { 3 String name; 4 int a1; 5 public void word() 6 { 7 System.out.println("工作"); 8 …

Selenium2+python自动化63-简易项目搭建

前言 到unittest这里基本上可以搭建一个简易的项目框架了&#xff0c;我们可以用一条run_main.py脚本去控制执行所有的用例&#xff0c;并生成报告&#xff0c;发送邮件一系列的动作 一、新建工程 1.打开pycharm左上角File>New Project&#xff0c;在Location位置输入项目名…

后端_服务器

本地搭建服务器 Nginx官网下载,解压放到本地文件夹.打开文件nginx.conf文件 ,做出以下修改:server {# 启动后的端口listen 8880; # 启动时的地址server_name localhost;# 启动后&#xff0c;地址栏输入: localhost:8880, 默认会在html文件夹下找 index.html文件locati…

2017年我国电力供需形势预测分析 清洁能源装机比重将提升

2016年&#xff0c;我国经济实现了“十三五”良好开局&#xff0c;GDP增速保持平稳&#xff0c;全社会用电量增速明显回升。2017年&#xff0c;我国面临着更为复杂的外部环境&#xff0c;经济下行压力仍然较大&#xff0c;电力供需形势将如何变化&#xff1f;有关专家进行了分析…

敏捷测试团队管理的挑战与机会

敏捷团队的管理其实的确面临着很多的挑战。蔡老师分别从敏捷管理的挑战、接受敏捷、敏捷下面的组织结构、敏捷架构下的沟通、敏捷下的KPI考核、以及机会和发展几个方面进行深入的讨论。 其实我觉得各个公司施行敏捷的时候都会遇见这次讲师所分享的一些问题&#xff0c;基本上都…

Taro-ui TabBar组件使用路由跳转

1、 安装taro-ui (此处使用cnpm) cnpm install taro-ui 2、 全局引入样式 app.scss sass &#xff1a;import "~taro-ui/dist/style/index.scss"; 3、 使用tabBar组件中引入AtTabBar &#xff0c;详情可查询官网&#xff1a;https://taro-ui.aotu.io/#/docs/tabbar i…