2014-05-10 23:26

题目链接

原题:

what is the best,worst and average case complexity for fibonacci no.s ..explain?

题目:计算斐波那契数的最好、最坏、平均复杂度是多少?

解法:计算斐波那契数倒是有好多方法,不过平均复杂度是怎么个说法?我写了三种解法:1. 白痴级的二路递归,复杂度是指数级的。2. 普通的递推算法,复杂度是线性的。3. 矩阵陈法,用快速幂可以达到对数级的时间。

代码:

 // http://www.careercup.com/question?id=5673934611546112
#include <iostream>
using namespace std; int fib1(int n)
{
if (n < ) {
return ;
} if (n == || n == ) {
return ;
} return fib1(n - ) + fib1(n - );
} int fib2(int n)
{
if (n < ) {
return ;
} if (n == || n == ) {
return ;
} int f1, f2, f3; f1 = f2 = ;
for (int i = ; i <= n; ++i) {
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
return f3;
} void matrixMultiply(int a[][], int b[][], int c[][])
{
int i, j, k; for (i = ; i < ; ++i) {
for (j = ; j < ; ++j) {
c[i][j] = ;
for (k = ; k < ; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
} void matrixPower(int a[][], int b[][], int n)
{
if (n < ) {
b[][] = b[][] = ;
b[][] = b[][] = ;
return;
} if (n == ) {
b[][] = a[][];
b[][] = a[][];
b[][] = a[][];
b[][] = a[][];
return;
} int p[][];
matrixPower(a, p, n / );
if (n % ) {
int c[][];
matrixMultiply(p, p, c);
matrixMultiply(a, c, b);
} else {
matrixMultiply(p, p, b);
}
} int fib3(int n)
{
if (n < ) {
return ;
} if (n == || n == ) {
return ;
} int a[][] = {
{, },
{, }
};
int b[][];
matrixPower(a, b, n - ); return b[][] + b[][];
} int main()
{
int n; while (cin >> n) {
cout << fib3(n) << endl;
} return ;
}

Careercup - Microsoft面试题 - 5673934611546112的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. div模拟下拉框

    1.模拟下拉框.点击文本框在文本框下面显示一个层divList,点击divList以外的任何地方,关闭divList层 document.body.onclick = function (e) { e ...

  2. sqlite mvc分页

      <tr class="tr1">                 <td>                     共<asp:Literal r ...

  3. 改变tabbar的高度做法

    UITabBarController *tabBarController =[[UITabBarController alloc] init]; CGRect frame = self.window. ...

  4. [leetcode]_Symmetric Tree

    第二道树的题目,依旧不会做,谷歌经验. 题目解释: give you a tree , judge if it is a symmetric tree. 思路:我以为要写个中序遍历(进阶学习非递归算法 ...

  5. cassandra 之 jdbc 使用【java、scala】

    1.数据库创建 参考接上文cassandra入门 http://www.cnblogs.com/piaolingzxh/p/4197833.html 2.下载jdbc驱动源码,构建jar包 源码下载地 ...

  6. Mysql查询(笔记二)

    1.两结构相同的表数据间移植 Inset into 表一 Select 字段1,字段2,....字段n from表二 建立数据库时设置数据库编码 create database 数据库名 charse ...

  7. Python学习教程(learning Python)--1.4 Python数据处理基础

    本节主要讨论数据操作及运算符等基础知识,熟悉C语言相关知识的读者请跳过此节. 在高级语言编程过程中,有了数据以后通常要对数据进行相应的数据处理,加.减.乘.除等基本运算,不难理解. 在Python里 ...

  8. WPF自定义控件(一)——Button

    接触WPF也有两个多月了,有了一定的理论基础和项目经验,现在打算写一个系列,做出来一个WPF的控件库.一方面可以加强自己的水平,另一方面可以给正在学习WPF的同行一个参考.本人水平有限,难免有一些错误 ...

  9. Node.js express路由简单分析

    这2天看了一点node+express的路由源码有了一点眉目,总结一下 对于app.get, 首先给出一张类图: 图1 注意每个路由有一个stack,这个stack中存放了Layer. 路由系统内有三 ...

  10. 比支付宝更好用的读IC卡信息工具

    编程语言:VC++ 更新时间:2014.10.23 操作系统:windowAll 工具:PCSC读卡器 在上一个博文<<解惑:NFC手机如何轻松读取银行卡信息?>>中,介绍了支 ...