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 ...
随机推荐
- ASP.NET内置对象一
ASP.NET提供了大量的对象类库,在这些类库中包含了许多封装好的内置对象,我们只需要直接使用这些对象的方法和属性,就能简单快速地完成很多的功能.Request对象.Response对象和Serve对 ...
- JavaScript高级 引用类型(二)《JavaScript高级程序设计(第三版)》
五.Function类型 是JS中最重要的一种引用类型 构造方式:(三种) 函数声明: 函数表达式定义: 函数构造器: 没有重载: 如果有两个相同函数名的函数,执行时,执行最近被定义的一次. 函数声明 ...
- Hadoop安装(Ubuntu Kylin 14.04)
安装环境:ubuntu kylin 14.04 haoop-1.2.1 hadoop下载地址:http://apache.mesi.com.ar/hadoop/common/hadoop-1. ...
- Knockout.Js官网学习(text绑定)
前言 text 绑定到DOM元素上,使得该元素显示的文本值为你绑定的参数.该绑定在显示<span>或者<em>上非常有用,但是你可以用在任何元素上. 简单绑定 Today's ...
- jquery实现radio按纽全不选和checkbox全选的实例
用jquery实现以下两个这个功能: 1.对所有单选按纽中radio全不选 单选按纽:<input type="radio" name="f1">A ...
- c#写入Mysql中文显示乱码 解决方法
如题,mysql字符集utf8,c#写入中文后,全部显示成?,一个汉字对应一个?解决方法:在数据库连接字符串中增加字符集的说明,Charset=utf8,如 MySQLConnection con = ...
- Java 第一天
环境变量设置(以JDK1.7为例) CLASSPATH=.\;C:\Program Files\Java\jdk1.7.0_45\lib\dt.jar;C:\Program Files\Java\jd ...
- VS2013+Qt5.6+VSaddin1.2.5
1 下载Qt(1)Qt安装包http://download.qt.io/official_releases/qt/(2)Qt插件http://ftp.jaist.ac.jp/pub/qtproject ...
- rails的字符编码
想练练手,随意的写了个登陆页面,简单的只有几行. <%= form_tag('login_check') do -%><%= text_field_tag 'user_name', ...
- NFC Forum : Frequently Asked Questions (NFC 论坛:FAQ)
NFC for Business What is the NFC Forum? The NFC Forum is a not-for-profit industry organization whos ...