數據結構與算法(周測1-算法分析)


判斷題

1.In a singly linked list of N nodes, the time complexities for query and insertion are O(1) and O(N), respectively.

     T      F

查找是O(N),因為需要沿着next指針找下去。而插入是O(1),只需要改變指針就行了。

2.If N numbers are stored in a singly linked list in increasing order, then the average time complexity for binary search is O(logN).

     T      F

因為鏈表不支持隨機存取,而O(logN)的算法嚴重依賴於隨機存取,所以不可能完成。

3.If keys are pushed onto a stack in the order abcde, then it's impossible to obtain the output sequence cedab.

     T      F

a在b的前面意味着a如果不在一開始取出就不能在b之前被取出,所以是不可能的。

4.An algorithm to check for balancing symbols in an expression uses a queue to store the partial expression.

     T      F

balancing symbols指的是一組匹配的符號,類似於圓括號,花括號,方括號。可見這道題的意思是求解逆波蘭表達式,使用stack而非queue。

5.To sort N distinct records by bubble sort, the number of record swaps must reach its maximum when the original sequence is almost in sorted order.

     T      F

冒泡排序的record swaps取決於逆序對的數量,對於一個已經排序過的序列,逆序對的數量可以是0或者最大,逆序對的數量可能最大也可能最小。

6.To sort N records by simple selection sort, the numbers of comparisons and record movements are O(N2) and O(N), respectively.

     T      F

比較次數的計算:(n-1)+(n-2)+...+2+1。共用n2/2次。移動只在一輪比較完成后,所以就算每次都需要移動,一共也才(n-1)次,所以比較可以看作O(N2),移動可以看作O(N)。

7.(neuDS)算法必須有輸出,但可以沒有輸入。

     T      F

算法有0個或多個輸入,以刻畫運算對象的初始情況;算法有一個或多個輸出,以反映對輸入數據加工后的結果。

8."Circular Queue" is defined to be a queue implemented by a circularly linked list or a circular array.

     T      F

循環隊列是一個抽象的概念,不局限於實現方式。

9.對於一個隨機算法,其最壞情況下的運行時間等於運行時間期望值的常數倍。

     T      F

雖然我們希望如此,因為這會使算法比較穩定,但大多數算法的運行時間期望值是最優和最差的情況的折中。

10.For a sequentially stored linear list of length N, the time complexities for query and insertion are O(1) and O(N), respectively.

     T      F

順序存儲的線性表支持隨機存取,所以查詢的時間是常數時間,但插入需要把后面每一個元素的位置都進行調整,所以是線性時間。

11.The Fibonacci number sequence {FN} is defined as: F0=0, F1=1, FN=FN-1+FN-2, N=2, 3, .... The space complexity of the function which calculates FN recursively is O(logN).

     T      F

為了求FN,需要從F0到FN的值,需要O(N)。

12.斐波那契數列FN的定義為:F0=0, F1=1, FN=FN-1+FN-2, N=2, 3, …。用遞歸函數計算FN的空間復雜度是O(logN)。

     T      F

13.(neuDS)數據的物理結構是指數據在計算機中的實際存儲形式。

     T      F

物理結構和計算機的硬件直接相關。

14.If keys are pushed onto a stack in the order {1, 2, 3, 4, 5}, then it is impossible to obtain the output sequence {3, 4, 1, 2, 5}.

     T      F

FIFO結構。

15.If the most commonly used operations are to visit a random position and to insert and delete the last element in a linear list, then sequential storage works the fastest.

     T      F

頻繁地對最后一個元素查刪,順序表完全可以勝任。

16.For the following piece of code

if ( A > B ){     
  for ( i=0; i<N*2; i++ )         
    for ( j=N*N; j>i; j-- )             
      C += A; 
}
else {     
  for ( i=0; i<N*N/2; i++ )         
    for ( j=N; j>i; j-- ) 
      for ( k=0; k<N*100; k++)
        C += B; 
} 

the lowest upper bound of the time complexity is O(N5)

     T      F

最多是O(N4)。

17.If keys are pushed onto a stack in the order abcde, then it's impossible to obtain the output sequence cdabe.

     T      F

18.If N numbers are stored in a doubly linked list in increasing order, then the average time complexity for binary search is O(logN).

     T      F
即使雙鏈表還是無法通過下標查詢,速度依然不可能到達O(logN)。

19.Given the input sequence onto a stack as {1, 2, 3, ..., N}. If the first output is i, then the j-th output must be j-i-1.

     T      F
除非能保證序列在完全入棧前都不出棧。

20.用動態規划而非遞歸的方法去解決問題時,關鍵是將子問題的計算結果保存起來,使得每個不同的子問題只需要被計算一次。子問題 的解可以被保存在數組或哈希散列表中。

     T      F

21.Let S be the set of activities in Activity Selection Problem. The greedy rule of "collecting the activity that starts the latest" is correct for finding a maximum-size subset of mutually compatible activities of S.

     T      F
貪婪算法適用於通過求解局部最優解獲得全局最優解的情況。和題目描述相符合。

22.(neuDS_C++)空串與空格串是相同的。

     T      F

23.(neuDS)線性表的邏輯順序和存儲順序總是一致的。

     T      F

24.(nueDS_C++)如果一個串中的所有字符均在另一串中出現,則說前者是后者的子串。

     T      F
注意串的序列性。

25.(neuDS)單鏈表不是一種隨機存取的存儲結構。

     T      F

26.設只包含根結點的二叉樹高度為0,則高度為k的二叉樹最小結點數為k+1。

     T      F
樹退化成線性表。

27.二叉樹通常有順序存儲結構和鏈式存儲結構。

     T      F

28.存在一棵總共有2016個結點的二叉樹,其中有16個結點只有一個孩子。

     T      F
假設二叉樹的結點擁有0個,1個,2個孩子的結點分別為N0,N1,N2。首先,需要了解,二叉樹中度為2的結點數量比葉節點少一,那么$N_{0}+N_{1}+N_{2}=2016 \Rightarrow N_{1}+2N_{2}+1=2016 \Rightarrow N_{1}+2N_{2}=2015$,看來N1必須是奇數。

29.具有10個葉結點的二叉樹中,有9個度為2的結點。

     T      F
參考上一題的分析。

30.An algorithm to check for balancing symbols in an expression uses a stack to store the symbols.

     T      F
參考第四題。

選擇題

1.If a linear list is represented by a linked list, the addresses of the elements in the memory:

     A.must be consecutive
     B.may or may not be consecutive
     C.must be consecutive for part of the elements
     D.must be nonconsecutive

2.If the most commonly used operations are to insert a new element after the last element, and to delete the first element in a linear list, then which of the following data structures is the most efficient?

     A.singly linked list
     B.singly linked circular list with a tail pointer
     C.singly linked circular list with a head pointer
     D.doubly linked list
循環鏈表的尾指針可以很快地找到頭指針,頭指針卻不能很快地找到尾指針

3.When is the linked list structure suitable for representing a linear list L?

     A.frequently insert into and delete from L
     B.frequently change the key values of the nodes in L
     C.L contains large amount of nodes
     D.the structure of the nodes in L is complicated
鏈表適合插入和刪除

4.The best "worst-case time complexity" for any algorithm that sorts by comparisons only is:

     A.O(logN)
     B.O(N)
     C.O(NlogN)
     D.O(N 2)
對於比較排序算法,堆排序和歸並排序的最壞時間復雜度都是O(NlogN)。

5.To sort N distinct elements in descending order by bubble sort, under which condition of the elements that the method will make the most number of swaps?

     A.pre-sorted in ascending order
     B.pre-sorted in descending order
     C.unsorted
     D.almost sorted
當數據是升序排序時,執行降序排序的逆序對數量最多,交換次數也最多。

6.To sort N elements by simple selection sort, the numbers of comparisons and movements are:

     A.O(N 2), O(N)
     B.O(N), O(logN)
     C.O(logN), O(N 2)
     D.O(NlogN), O(NlogN)
參考判斷題第六題對選擇排序的分析。

7.Use simple insertion sort to sort 10 numbers from non-decreasing to non-increasing, the possible numbers of comparisons and movements are:

     A.100, 100
     B.100, 54
     C.54, 63
     D.45, 44
插入排序的比較次數為$(n-1)+(n-2)+...+1=\frac{n(n-1)}{2}$,交換次數一定比比較次數低,所以選擇D。

8.Since the speed of a printer cannot match the speed of a computer, a buffer is designed to temperarily store the data from a computer so that later the printer can retrieve data in order. Then the proper structure of the buffer shall be a:

     A.stack
     B.queue
     C.tree
     D.graph

9.Represent a queue by a singly linked list. Given the current status of the linked list as 1->2->3 where x->y means y is linked after x. Now if 4 is enqueued and then a dequeue is done, the resulting status must be:

     A.1->2->3
     B.2->3->4
     C.4->1->2
     D.the solution is not unique

10.Use binary search to find a number from 100 sorted numbers, the worst-case number of comparisons is:

     A.7
     B.10
     C.50
     D.99

11.Given the rucurrent equations for the time complexity of a program as: T(1)=1, and T(N)=2T(N/2)+N. Then the time complexity must be:

     A.O(logN)
     B.O(N)
     C.O(NlogN)
     D.O(N 2)

為了獲得T(N),需要計算遞歸等式logN次,2T(N/2)計算logN次后就等於N,N計算logN次后等於NlogN,兩邊取大的一方,最終的結果就是NlogN。

12.The recurrent equations for the time complexities of programs P1 and P2 are:

  • P1: T(1)=1, T(N)=T(N/2)+1;
  • P2: T(1)=1, T(N)=2T(N/2)+1;

Then the correct conclusion about their time complexities is:

     A.they are both O(logN)
     B.O(logN) for P1, and O(N) for P2
     C.they are both O(N)
     D.O(logN) for P1, and O(NlogN) for P2

P1的加號左邊經過logN次遞歸等式計算恆等於1,而右邊每次遞歸等式都加1,經過logN次計算最后為logN,兩邊取大是logN;

P2加號的左邊經過logN次遞歸等式計算是N,右邊每次遞歸等式都加1,經過logN次計算最后為logN,兩邊取大總體為N。

13.For a sequentially stored linear list of length N, the time complexities for query and insertion are:

     A.O(1), O(1)
     B.O(1), O(N)
     C.O(N), O(1)
     D.O(N), O(N)

14.For a sequentially stored linear list of length N, which of the following operations has the time complexity O(1)?

     A.visit the i-th (1≤i≤N) node and find the immediate predecessor of the i-th (2≤i≤N) node
     B.insert a new node after the i-th (1≤i≤N) node
     C.delete the i-th (1≤i≤N) node
     D.sort the N nodes in increasing order

15.切原木問題:給定一根長度為N米的原木;另有一個分段價格表,給出長度L=1,2,...,M對應的價格PL。要求你找出適當切割原木分段出售所能獲得的最大收益RN。例如,根據下面給出的價格表,若要出售一段8米長的原木,最優解是將其切割為2米 和6米的兩段,這樣可以獲得最大收益R8=P2+P6=5+17=22。而若要出售一段3米長的原木,最優解是 根本不要切割,直接售出。

Length L 1 2 3 4 5 6 7 8 9 10
Price PL 1 5 8 9 10 17 17 20 23 28

下列哪句陳述是錯的?

     A.此問題可以用動態規划求解
     B.若N≤M,則有R N=max{P N,max 1<=i&ltN{R i+R N-i}}
     C.若N>M,則有R N=max 1<=i&ltN{R i+R N-M}
     D.算法的時間復雜度是O(N 2)
N&gtM時,應該為RN=max1<=i {R i+R M-i}

16."二叉樹為空"意味着二叉樹() 。

     A.由一些沒有賦值的空結點構成
     B.根結點沒有子樹
     C.不存在
     D.沒有結點

17.棧和隊列的共同點( )。

     A.都是先進先出
     B.都是后進先出
     C.只允許在端點處插入和刪除元素
     D.沒有共同點

18.廣義表是一種()數據結構。

     A.非遞歸的
     B.遞歸的
     C.樹型
     D.圖狀
廣義表容許表中的元素擁有自己的數據結構,使用遞歸的方式定義,所以是遞歸的數據結構。

19.若串S="software",其子串的數目是

     A.8
     B.37
     C.36
     D.9
子串包含空串。

20.What is the major difference among lists, stacks, and queues?

     A.Lists use pointers, and stacks and queues use arrays
     B.Stacks and queues are lists with insertion/deletion constraints
     C.Lists and queues can be implemented using circularly linked lists, but stacks cannot
     D.Lists are linear structures while stacks and queues are not

21.線性表是一個()。

     A.有限序列,可以為空
     B.有限序列,不能為空
     C.無限序列,可以為空
     D.無限序列,不能為空

22.Given a 12×12 symmetric matrix M. If the upper triangular entries mi,j (1≤i≤j≤12) of M are stored row by row in a 1-dimentional array N (in C programming language). What is the index of m6,6 in N?

     A.50
     B.51
     C.55
     D.66
上三角每一行的第一個元素為mi,i,所以m6,6為第12+11+10+9+8+1=51個元素,因為數組下標從0開始,所以最后得到的下標為50。

23.閱讀下列程序,其功能是()。

typedef struct {
ElemType *list;
int size;
intMaxSize;
}SeqList;
void fun1(SeqList&L) {
inti, j;
ElemType temp;
   for (i=0, j= L.sise-1; i<j; i++, j--) {
       temp=L.list[i];
       L.list[i]=L.list[j];
       L.list[j]=temp;
   }
}
     A.將順序表原地逆置
     B.將鏈表原地逆置
     C.將順序表首尾元素對換
     D.將鏈表首尾元素對換

24.If the pushing sequence of a stack is {1, 2, 3, ..., N} and the first number being popped out is i, then the j-th number being popped must be:

     A.i?j?1
     B.i?j
     C.j?i?1
     D.cannot be determined

25.In-order traversal of a binary tree can be done iteratively. Given the stack operation sequence as the following:

push(1), push(2), push(3), pop(), push(4), pop(), pop(), push(5), pop(), pop(), push(6), pop()

Which one of the following statements is TRUE?

     A.3 and 5 are siblings
     B.1 is the parent of 5
     C.6 is the root
     D.None of the above
這道題本質上講的是中序遍歷的非遞歸實現,根據給出的stack序列,能夠構建出一個二叉樹。 ![](https://img2018.cnblogs.com/blog/1694164/201910/1694164-20191029212412099-1674792758.png)

26.Given an empty stack S and an empty queue Q. Push elements {1, 2, 3, 4, 5, 6, 7} one by one onto S. If each element that is popped from S is enqueued onto Q immediately, and if the dequeue sequence is {3, 2, 6, 5, 7, 4, 1}, then the minimum size of S must be:

     A.2
     B.3
     C.4
     D.5
棧元素數量最大的時候是1,4,5,6。

27.What is NOT a feature of a linked list?

     A.do insertion and deletion without having to move the elements
     B.easy for random query
     C.no pre-estimation of the size is necessary
     D.the space taken is proportional to the size of the list

28.Suppose that an array of size m is used to store a circular queue. If the front position is front and the current size is size, then the rear element must be at:

     A.`front+size`
     B.`front+size-1`
     C.`(front+size)%m`
     D.`(front+size-1)%m`
考慮滿的情況,size==m,front應該比rear少1,所以明顯選D。

29.Suppose that enqueue is allowed to happen at both ends of a queue, but dequeue can only be done at one end. If elements are enqueued in the order {a, b, c, d, e}, the impossible dequeue sequence is:

     A.b a c d e
     B.d b a c e
     C.e c b a d
     D.d b c a e
這道題考慮序列的時候只要反着思考,從a出發,如果這個序列可以被有序地拿掉,那么就是合法地,D中a的左右兩邊都和a不連續,明顯錯了。

30.依次在初始為空的隊列中插入元素a,b,c,d以后,緊接着做了兩次刪除操作,此時的隊頭元素是( )。

     A.a
     B.b
     C.c
     D.d


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM