Careercup - Microsoft面试题 - 5673934611546112
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的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
随机推荐
- 浅谈C++虚函数
很长时间都没写过博客了,主要是还没有养成思考总结的习惯,今天来一发. 我是重度拖延症患者,本来这篇总结应该是早就应该写下来的. 一.虚函数表 C++虚函数的机制想必大家都清楚了.不清楚的同学请参看各种 ...
- c++库大全
1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介绍——准标准库Boost 3.C++各大有名库的介绍——GUI 4.C++各大有名库的介绍——网络通信 5.C++各大有名库的介绍— ...
- idea类似eclipse鼠标提示java api信息
<ignore_js_op> 详细说明:http://java.662p.com/thread-2615-1-1.html
- sql取字段特定符号的前/后
declare @canshu varchar(200)set @canshu='24§咨询客户'--某符号之后的字段内容select substring(@canshu,charindex('§', ...
- (一)、NodeJS (转载)
NodeJS基础 ------ 转载自阿里巴巴 什么是NodeJS JS是脚本语言,脚本语言都需要一个解 ...
- POJ C++程序设计 编程题#1 编程作业—运算符重载
编程题 #2 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的MyIn ...
- sender是什么意思C#
/// <summary> /// sender就是事件发起者,e存储事件发起者的一些参数 /// 例如: /// private void button1_Click(object se ...
- 判断字符串是否包含字母‘k’或者‘K’
判断字符串是否包含字母‘k’或者‘K’ public bool IsIncludeK(string temp) { temp = temp.ToLower(); if (temp.Contains(' ...
- 将Tab栏居中的方法
原始tab: 居中后的tab(边缘效果是截图的问题): 改变方法如下: 找到Android SlidingTabLayout源代码,在Android SlidingTabLayout源代码中有一个方法 ...
- 16)JAVA实现回调(Android,Swing中各类listener的实现)
熟悉MS-Windows和X Windows事件驱动设计模式的开发人员,通常是把一个方法的指针传递给事件源,当某一事件发生时来调用这个方法(也称为"回调").Java ...