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 ...
随机推荐
- 逻辑回归的分布式实现 [Logistic Regression / Machine Learning / Spark ]
1- 问题提出 2- 逻辑回归 3- 理论推导 4- Python/Spark实现 # -*- coding: utf-8 -*- from pyspark import SparkContext f ...
- 基于s5pv210嵌入式linux使用其他动态、静态库文件程序的交叉编译
刚刚移植了sqlite3迫切想测试一些,结果将原来在ubuntu系统下写好且测试通过的程序,重新编译就报错,无法找到已定义的函数 这是由于没有使用库或者使用了错误的就.库造成的结果. 正确做法为: a ...
- C puzzles详解【38-45题】
第三十八题 What is the bug in the following program? #include <stdlib.h> #include <stdio.h> # ...
- Ubuntu14.04进入文本模式方法
ubuntu 10.10以前的操作方法: 1 第一步,具体命令及操作如下: sudo vi /etc/init/rc-sysinit.conf env DEFAULT_RUNLEVEL=3 <- ...
- sublime搭建Java编译平台及编码问题
Sublime自带Java编译功能,当时只能编译不能运行,我们做一下小小的修改就可以让sublime一步完成编译运行的功能,实现sublime搭建Java编译平台. 使用Ctrl + B 编译时,所编 ...
- jquery 仿手机屏幕切换界面效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Custom Sort Order
When trying to sort based on values that do not fit the standard ascending and descending sort logic ...
- Silverlight取得Session
首先Session是运行在服务器上的,而Silverlight运行在客户端.因此在Silverlight中使用SESSION的说法并不准确, 只因大家经常这样搜索才起这个名字. 有两种方法实现Silv ...
- [译]MongoDB 3.0发布说明
原文来自:http://docs.mongodb.org/manual/release-notes/3.0/ 2015年3月3日 MongoDB 3.0现已可供使用.关键新特性包括支持WiredTig ...
- 通过HttpClient方式连接网络
xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...