第二章 Big O notation 试题以及讲解 (包会)

这里列出了一些难做的题或是易错的题,简单的我就没有过多的解释,但是如果大家有任何问题都可以私信我或是评论一下,我会尽量即时的解答问题或是疑问的。

int x=0;

for(int i=4*n; i>=1; i--)

x=x+2*i;

O(n)

The loop runs O(n) times and does O(1) work per iteration.

int z=0;

int x=0;

for (int i=1; i<=n; i=i*3){

z = z+5;

z++;

x = 2*x;

}

O(log n)

Think about the values of i as the loop progresses. It will take on the series of values 1, 3, 9, 27, 81, 243, ..., 3k. Since i is tripling on each iteration, it takes on successive powers of three.

The loop clearly only does O(1) work per iteration, so the main

question here is how many total iterations there will be. The loop

will stop when i > n. If we let k be some arbitrary iteration of the

loop, the value of i on iteration k will be 3k. The loop stops when

3k > n, which happens when k > log3 n. Therefore, the number of

iterations is only O(log n)

int y=0;

for(int j=1; j*j<=n; j++)

j <= (n)^1/2 y++;

O(√n)

Notice that j is still growing linearly, but the loop runs as long as

j2 ≤ n. This means that as soon as j exceeds √ n, the loop will

stop. Therefore, there will only be O(√n) iterations of the loop,

and since each one does O(1) work, the total work done is O(√n)

int b=0; //constant

for(int i=n; i>0; i--)

for(int j=0; j<i; j++)

b=b+5;

O(n^2)

The most accurate answer would be O(n2)

int y=1;

int j=0;

for(j=1; j<=2n; j=j+2)

y=y+i;

int s=0;

for(i=1; i<=j; i++)

s++;

O(n)

int b=0;

for(int i=0; i<n; i++)

for(int j=0; j<i*n; j++)

b=b+5;

The inner loop will run 0 + n + 2n + 3n + 4n + ... + n(n-1) = n(0 + 1

+ 2 + ... + n - 1) times, so the total work done is O(n3). You

shouldn‘t multiply by the number of times the outer loop runs because

you‘re already summing up across all iterations. The most accurate

runtime would be O(n3)

int t=0;

for(int i=1; i<=n; i++)

for(int j=0; j*j<4*n; j++)

for(int k=1; k*k<=9*n; k++)

t++;

Look at the second loop. This actually runs O(√n) times using the

same logic as one of the earlier parts. That third inner loop also

runs O(√n) times, and so the total work done will be O(n2)

int a = 0;

int k = n*n;

while(k > 1)

{

for (int j=0; j<n*n; j++)

{ a++; }

k = k/2;

}

The outer loop starts with k initialized to n2, but notice that k is

halved on each iteration. This means that the number of iterations of

the outer loop will be log (n2) = 2 log n = O(log n), so the outer

loop runs only O(log n) times. That inner loop does do O(n2) work, so

the total runtime is O(n^2 log n)

int i=0, j=0, y=0, s=0;

for(j=0; j<n+1; j++)

y=y+j;

for(i=1; i<=y; i++)

s++;

O(n^3)

时间: 2024-08-04 10:53:51

第二章 Big O notation 试题以及讲解 (包会)的相关文章

第二章 Big O notation 进阶课程

第二章 Big O notation 进阶课程 在这一章中,我们将会了解到算法的基础----Counting primitive operation,什么是primitive operation: The following are all primitive operations: 1. Assigning a value to a variable 2. Calling another algorithm (function) 3. Performing an arithmetic oper

第二章 Big O notation

第二章 Big O notation 计算机的计算过程中,基本上都是一些复杂的计算,数以千计,数以万计或是数以亿计的计算,那么如何计算和总结为让我们更加简单易懂的语言呢,与成绩分层是一个道理,A是好的,B次好等等等等,那么我们就引入了big O notation这个概念. 在这里,我们程序员如果要进行编程,我们不希望计算机花费大量的时间去进行一个运算,对于我们和用户来说,我们要尽全力将big O 弄到最小. 1,2,3,4,5,6,7的是O(1),是属于常数的 n,2n,2n+1,4m+4的是O

2014年软考-信息技术处理员-模拟试题及答案【第二章】

51CTO学院,在软考备考季特别整理了"2014年软考信息技术处理员模拟试题及答案[汇总篇]",帮助各位学院顺利过关!更多软件水平考试辅导及试题,请关注51CTO学院-软考分类吧! 查看汇总:2014年软考-信息技术处理员-模拟试题及答案[汇总篇]  ●计算机网络的主要目标是实现____(16 )__C__. A.数据处理 B.文献检索 C.资源共享和信息传输 D.信息传输 ●Internet上,访问Web网站时用的工具是浏览器.下列____(17 )_A___就是目前常用的Web浏览

数学建模学习笔记(第二章:7个入门建模实例讲解)

第二章:初等模型(初等数学方法建模) 1.    席位分配: a)      问题描述:三个系学生共200名(甲系100.乙系60,丙系40).代表会议共20席,按比例分配,三个系分别为10,6,4席. b)     问题存在:现因学生转系,三系人数分别变为:103,63,34.问20个席位如何分配?才能使得尽量"公平". c)      解决方法:提出不同的假设,进行不同方法的讨论,对不同方法进行对比分析(满足哪些公平条件),得出结论. 2.    双层玻璃窗的功效: a)     

一维向量旋转算法 编程珠玑 第二章

看了编程珠玑第二章,这里面讲了三道题目,这里说一下第二题,一维向量旋转算法. 题目:将一个n元一维向量(例数组)向左旋转i个位置. 解决方法:书上讲解了5种方法,自己只想起来2种最简单方法(下面讲的前两种). 1.原始方法. 从左向右依次移动一位,对所有数据平移:这样循环i次,算法最坏时间复杂度达n^2.耗时不推荐. 2.空间换时间. 顾名思义,申请一个i长度的空间,把前i半部分放到申请空间中,再把后面的所有数据向左移动i个位置,最后把申请的空间中的数据放到后半部分.浪费空间,不推荐. 3.杂技

Java基础知识二次学习-- 第二章 基础语法与递归补充

第二章 基础语法与递归补充   时间:2017年4月24日10:39:18 章节:02章_01节,02章_02节 视频长度:49:21 + 15:45 内容:标识符,关键字与数据类型 心得:由字母,下划线,$,数字组成,应该由字母,下划线$开头,同时应该避开java保留字符 变量是内存中的一小块区域,使用变量名来访问这块区域 执行过程中的内存管理(疑问:这里的内存和Jvm的一样吗?) code segment 存放代码 data segment 静态变量 字符串常量 stack 栈 局部变量 h

Android开发艺术探索——第二章:IPC机制(上)

Android开发艺术探索--第二章:IPC机制(上) 本章主要讲解Android的IPC机制,首先介绍Android中的多进程概念以及多进程开发模式中常见的注意事项,接着介绍Android中的序列化机制和Binder,然后详细的介绍Bundle,文件共享,AIDL,Messenger,ContentProvider和Socker等进程间通讯的方法,为了更好的使用AIDL进行进程间通讯,本章引入了Binder连接池的概念,最后,本章讲解各种进程间通信方式的优缺点和使用场景,通过本章,可以让读者对

Android艺术开发探索——第二章:IPC机制(下)

Android艺术开发探索--第二章:IPC机制(下) 我们继续来讲IPC机制,在本篇中你将会学习到 ContentProvider Socket Binder连接池 一.使用ContentProvider ContentProvider是Android中提供的专门用来不同应用之间数据共享的方式,从这一点来看,他天生就是适合进程间通信,和Messenger一样,ContentProvider的底层实现同样也是Binder,由此可见,Binder在Android系统中是何等的重要,虽然Conten

读大道至简第二章有感

大道至简第二章的题目是"是懒人造就了方法"而开头也写到李冰的开山并与愚公移山做了比较,无非就是想用具体的例子来证明懒人造就方法的观点.而其深层的含义便是要学会观察,学会思考,同样是一件事,很多人都能做,有人用的时间长资源多,而有的人则截然相反,这就说明了观察思考的重要性,而相对于编程来说,一个简单的比较大小的问题,有的人比较五次,而有的人只比较四次就能运行出结果,这就是程序的优化,也是思考的结果 一百万行代码是可以写在一个文件里的.这反映了一个很常见的问题,很多初学者比如我们总是在关注