資料結構小考解答PPT

advertisement
資料結構
第一次小考解答
1.(16%)Determine whether the
following statements are correct
 以定義判斷
10nlogn+12n2+8n ≤ C(n2)
存在 C=22, n0 = 2
當n ≧n0 時, 10nlogn+12n2+8n ≤ C(n2)成立
本題為True
 找等式左邊成長速度最快的是不是3n
15n3n常數乘法可以忽略
只討論n3n
n3n比3n成長速度還快 ,所以本題為False
 找等式左邊成長速度最快的是不是 nlogn
5n2常數乘法可以忽略
只討論n2
n2比nlogn成長速度還快 ,所以本題為False
 以定義判斷
8n5+3n≧C(2n)
存在 C=1, n0 = 1
當n ≧n0 時, 8n5+3n≧C(2n)成立
本題為True
2.(10%)Compute the failure function for
pattern “aabaababaa”
 failure function初始值為-1
 與前一項『 failure function 值+1』項做比較
相等 :failure function = 前一項『 failure function 值+1』
不相等: failure function= -1
3.(10%) Please write an algorithm to calculate the
result of a postfix expression.
 Algorithm定義
1.一定要有Input 2.每一指令清楚說明要做什麼 3.在有限的步驟結束
本題解答
 由左到右讀取 post expression
3分
 當讀取到數字則push進stack
3分
 當讀取到運算符號,pop出兩個數字並做運算,再把結果
push進stack
4分
4.(10%) Please analyze the time complexity (in
terms of q()) of the following program.
int F(int n)
{ int x=n;
if (n<=1)
return n;
else
{for (int i=0; i< n; i++)
x=x+i;
return x; }}
+1
+1
+1
+1
+( n+1 )
+n
+1
5.If following declaration is used to implement a “circular
queue”, please answer the following questions.
int queue[MAX_QUEUE_SIZE];
int front,rear;
 a.(4%) Initially, what values will you assign to front and rear?
Front = Rear = 0 各2分
 b.(10%) When inserting and deleting, please state the equations that
you use to change the values of front and rear.
寫出insert和delete時 rear 跟front的通式
為了防止超出Queue的範圍 需mod 最大容量
Insert: rear = (rear + 1) % [MAX_QUEUE_SIZE];
delete: front = (front + 1) % [MAX_QUEUE_SIZE];
5分
5分
 c.(6%) How do you judge the queue is empty or full?
Empty: front = rear
3分
Full : rear = (rear + 1) % [MAX_QUEUE_SIZE] = front 3分
 d.(4%) According to your judge statements for stack full and stack
empty, how many data items can be stored simultaneously in the
queue?
為了判斷是否為Full需空出一格不存資料
[MAX_QUEUE_SIZE]-1
[2]
[3]
B
[1]
front
C
A
[0]
D [4]
E
[5]
rear
6.(6%) int a[25][10][20];
Assume the address of a[0][0][0] is a. Please compute the
address of a[8][5][10].
 a[25] [10] [20]
U1 U2 U3
a[8] [5] [10]
i1 i2 i3
7.Convert the following infix
expressions into postfix expressions
 a.(5%) a+b*c/d+e/f
=a+T1/d+e/f
= a+T2+e/f
= a+T2+T3
=T4+T3
=T5
 T5=T4T3+
=T4 ef/+
= aT2+ ef/+
= a T1d/+ ef/+
= abc*d/+ ef/+
T1=bc*
T2=T1d/
T3=ef/
T4=aT2+
T5=T4T3+
 b.(5%) (a+b*c/d)-(e/f+c*d)
= (a+T1/d)-(e/f+c*d)
=(a+T2)-(e/f+c*d)
=T3-(e/f+c*d)
= T3-(T4+c*d)
= T3-(T4+T5)
= T3-T6
=T7
 T7=T3T6-
= aT2+ T6= a T1d/+ T6= abc*d/+ T6= abc*d/+ T4T5+= abc*d/+ ef/ cd*+-
T1=bc*
T2= T1d/
T3= aT2+
T4=ef/
T5=cd*
T6=T4T5+
T7=T3T6-
class MatrixTerm
{ friend class SparseMatrix
private:
int row, col, value; };
class SparseMatrix { private:
int Rows, Cols, Terms;
MatrixTerm smArray[101];};
Using the structure declared about, please write
a. (10%) A pseudo code for matrix addition.
b. (10%) A pseudo code for transposing a matrix. Moreover,
the time complexity of your method should no greater than
O(column*row) (Please verify your result).
Download