115 年 國立中正大學資訊工程學系碩士班《軟體設計》
For Questions 1 to 24, select multiple correct answers. Choose all (one or more) that apply. No partial credit is given.
第 1 題3 分
If you want to store the string literal "123456", which string variable can be used to store this string successfully?
(A) char str1[5];
(B) char str2[6];
(C) char str3[7];
(D) char str4[8];
(E) char str5[9];
(F) none of the above
登入後即可作答並保存紀錄。
核心觀念
在 C 語言中,字串以字元陣列儲存,並以結尾字元 '\0' 標示字串結束。字串常值 "123456" 包含 6 個可見字元,還需要 1 個位置存放 '\0',因此至少需要 7 個 char 空間。
解題方法
先計算字串常值中的字元數,再加上結尾的 '\0',得到陣列所需的最小長度。陣列長度大於或等於 7 才能完整儲存此字串。
選項分析
- (A)
char str1[5];:錯誤。 陣列只有 5 格,連 6 個可見字元都放不下。 - **(B)
char str2[6];:錯誤。
第 2 題3 分
The following C program is intended to output the message "The sum is: 30." However, the current program produces incorrect output or undefined behavior. Which of the following statements correctly explain the errors in the main function?
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
printf("The sum is: " + a + b);
return 0;
}
(A) The variable a should be declared as a float variable instead of an int variable.
(B) The + operator cannot be used in the function call of printf.
(C) The printf function should use a format specifier in its format string.
(D) The return statement should return the sum of a and b.
(E) The format string and the integer expression a + b should be separated by a comma, not a + operator.
(F) none of the above
登入後即可作答並保存紀錄。
核心觀念
printf 的第一個引數是格式字串,後續引數則提供要輸出的資料。若要輸出整數,格式字串須包含 %d,並以逗號傳入整數運算式,例如:
printf("The sum is: %d.", a + b);
C 語言中的字串常值在運算式中會轉成指向其首字元的指標。因此,"The sum is: " + a + b 會被解讀為對字串指標做指標位移,不是把字串和整數相加後輸出。
解題方法
已知 a = 10、b = 20。檢查原程式的 printf 呼叫:
printf("The sum is: " + a + b);
+ 運算子由左至右結合,等同於:
printf(("The sum is: " + a) + b);
字串常值長度為 12 個字元,另有結尾的空字元 '\0'。指標先位移 10 個位置,再位移 20 個位置,超出該字串陣列允許的範圍;這會造成未定義行為。即使只看呼叫形式,printf 也只收到一個引數,沒有收到要輸出的整數值。
正確寫法是:
int a = 10;
int b = 20;
printf("The sum is: %d.", a + b);
第 3 題4 分
Which of the following statements regarding C operators are correct?
(A) Given an integer variable a with an initial value of , the expression a++ increments the value of a after the current value is used in the expression.
(B) The modulus operator % can be used with float or double types.
(C) The return value of (5 < 10) && (15 < 10) || !(5 != 0) is .
(D) The return value of !(1 < 2) && 2 / 1 || 0.5 / 2 || 10 % 3 is .
(E) The precedence of the relational operators is higher than that of the arithmetic operators.
(F) none of the above
登入後即可作答並保存紀錄。
核心觀念
本題考查 C 語言的遞增運算子、運算子適用型別、優先順序,以及邏輯運算的短路特性。
- 後置遞增
a++的運算結果是a原本的值,並在該值計算後使a加 。 %是整數餘數運算子,運算元須為整數型別。- 關係運算子
<、>等的結果為整數值 或 ;邏輯運算子!、&&、||的結果也為 或 。 - 算術運算子的優先順序高於關係運算子;關係運算子的優先順序高於
&&,而&&又高於||。 &&與||會短路:結果已能確定時,後續運算元不會執行。
解題方法
逐一依照 C 語言的運算子規則判斷。遇到複合運算式時,先依優先順序加上括號,再由左至右計算;對 &&、|| 額外檢查是否短路。
選項分析
(A) 正確。 a++ 是後置遞增。若 a 初值為 ,此運算式的值是原本的 ,運算後 a 變為 。
(B) 錯誤。 % 僅適用於整數型別,不能用於 float 或 double。浮點數取餘數須使用標準函式庫中的 fmod 等函式。
(C) 正確。 先計算關係運算,再計算 &&,最後計算 ||:
第 4 題5 分
What are the correct expressions for blanks (1), (2), and (3) in the C program below to make the array num contain the values ?
#include <stdio.h>
int main()
{
int num[2][3] = {6, 5, 4, 3, 2, 1};
int (1), *pt;
for (p = num; (2); p++) {
(3);
}
return 0;
}
(A) (1) (*p)[3]; (2) p <= num + 1; (3) (*p)[2] = 0
(B) (1) (*p)[2]; (2) p <= num + 2; (3) (*p)[3] = 0
(C) (1) *p[3]; (2) p <= num + 1; (3) *p[2] = 0
(D) (1) *p[2]; (2) p <= num + 2; (3) *p[3] = 0
(E) None of the options is correct.
登入後即可作答並保存紀錄。
核心觀念
二維陣列 int num[2][3] 是「含有 2 個元素的陣列」,每個元素都是含有 3 個 int 的陣列。運算式中的 num 會轉成指向第一列的指標,型別為 int (*)[3],也就是「指向含 3 個 int 的陣列之指標」。
宣告 int (*p)[3] 中,p 是指標;(*p)[3] 則表示它指向長度為 3 的 int 陣列。括號不可省略,因為 [] 的優先序高於 *。
解題方法
初始值依列優先順序填入陣列:
要得到 ,必須將每一列的第 3 個元素,也就是索引 2 的元素設為 0。因此,p 應指向每一列,並且走訪兩列:
p = num:指向第 0 列。p <= num + 1:走訪第 0 列與第 1 列。(*p)[2] = 0:將目前列的第 3 個元素設為0。
關鍵程式碼如下:
int (*p)[3], *pt;
for (p = num; p <= num + 1; p++) {
(*p)[2] = 0;
}
執行後,num 的內容為:
第 5 題5 分
Which of the following statements regarding bitwise operators in C are correct?
(A) Given a 16-bit unsigned integer variable i with the assigned value , the return value of i << 2 is .
(B) Given a 16-bit unsigned integer variable i with the assigned value 0x0010, the return value of i >> 3 is .
(C) Given two 16-bit unsigned integer variables i with the assigned value and j with the assigned value , the return value of i ^ j is .
(D) Given two 16-bit unsigned integer variables i with the assigned value and j with the assigned value , the return value of i | j is .
(E) Given a 16-bit unsigned integer variable i with the assigned value 0x0030, the return value of i | 2 << 2 is .
(F) none of the above
登入後即可作答並保存紀錄。
核心觀念
位元運算直接比較整數的二進位位元:
<<:左移,空出的低位補 ;在不發生溢位的情況下,左移 位相當於乘以 。>>:右移,對非負整數而言,右移 位相當於除以 取整數部分。^:逐位互斥或(XOR),兩位不同時結果為 。|:逐位或(OR),任一位為 時結果為 。
C 語言中,<< 的運算子優先序高於 |,所以 i | 2 << 2 會解讀成 i | (2 << 2)。
解題方法
將各數值寫成二進位,按照運算子逐位計算;涉及位移時,確認移動位數與移位後的數值。這些選項的數值都沒有造成溢位,因此可直接依位元運算規則判斷。
選項分析
(A) 錯誤。
的二進位為 ,左移兩位:
因此結果是 ,不是 。
(B) 錯誤。
0x0010 等於十進位 ,二進位為 。右移三位:
因此結果是 ,不是 。
第 6 題5 分
What are the correct expressions for blanks (1) and (2) to make the following program output the information stored in s1 correctly?
#include <stdio.h>
struct item {
char name[100];
char date[10];
};
typedef struct {
struct item product[2];
int row;
} Shelf;
int main()
{
Shelf s1 = {
.product[0] = {"A Little Life", "20150310"},
.product[1] = {"Taiwan Travelogue", "20200331"},
.row = 2
};
Shelf *s1_ptr = &s1;
int num = sizeof(s1.product) / sizeof(s1.product[0]);
for (int i = 0; i < num; i++) {
printf("Product Name: %s; ", ____(1)____);
printf("date: %s\n", ____(2)____);
}
return 0;
}
(A) (1) s1.product[i].name; (2) s1.product[i].date
(B) (1) s1.product[i]->name; (2) s1.product[i]->date
(C) (1) *s1_ptr.product[i].name; (2) *s1_ptr.product[i].date
(D) (1) (*s1_ptr).product[i].name; (2) (*s1_ptr).product[i].date
(E) (1) s1_ptr->product[i]->name; (2) s1_ptr->product[i]->date
(F) none of the above
登入後即可作答並保存紀錄。
核心觀念
本題考查結構(struct)、結構陣列、指標,以及 . 與 -> 成員運算子的使用。
s1是Shelf結構變數,因此以s1.member存取成員。s1_ptr是指向Shelf的指標,因此可用s1_ptr->member,也可先解參考再用(*s1_ptr).member。product是結構陣列,product[i]的型別是struct item,不是指標;因此要用product[i].name與product[i].date存取欄位。name和date是字元陣列,以%s輸出時直接傳入陣列名稱即可。
解題方法
先辨認各變數的型別:s1 是 Shelf,s1_ptr 是 Shelf *;而 s1.product[i] 是 struct item。據此可得兩種正確存取方式:
s1.product[i].name
s1.product[i].date
或透過指標存取:
(*s1_ptr).product[i].name
(*s1_ptr).product[i].date
其中 (*s1_ptr) 外的括號不可省略,因為 . 的優先順序高於一元 *。本題的迴圈上限為:
因此程式會依序輸出兩筆商品資料。關鍵程式碼如下:
第 7 題4 分
What is the return type of a class constructor?
(A) An instance of the class.
(B) No return on a constructor.
(C) An int (the size of the class in bytes).
(D) A string (the name of the class).
(E) none of the above
登入後即可作答並保存紀錄。
核心觀念
建構子(constructor)是建立物件時負責初始化物件的特殊成員。以常見的 Java、C++ 等物件導向語言來說,建構子本身沒有宣告回傳型別;建立物件的運算式會取得新物件的實例,但那不是建構子回傳的值。
解題方法
先區分「建構子」與「建立物件的運算式」:
- 建構子負責初始化物件,沒有回傳型別。
- 建立物件的運算式(例如 Java 的
new ClassName())會產生並取得該類別的實例。
因此,題目問的是建構子的回傳型別,應選表示「建構子不宣告回傳型別」的選項。
選項分析
- (A) An instance of the class. 錯。類別實例是建立物件的運算式所取得的結果,不是建構子的回傳型別。
第 8 題4 分
Which of the following is NOT true about the special variable “this”?
(A) It is a reference value.
(B) You are allowed to change what it points to within a method.
(C) It points to the calling instance of a method.
登入後即可作答並保存紀錄。
核心觀念
this 是物件導向程式中指向目前物件的特殊參考。在實例方法內,它代表呼叫該方法的物件,可用來存取該物件的欄位與方法。
this 的值由方法呼叫時的物件決定;方法內可以透過 this 使用目前物件,卻不能把 this 改指向另一個物件。
解題方法
逐項檢查敘述是否符合 this 的兩個特性:它代表目前物件的參考,而且在方法內不可重新指定其所指向的物件。題目問「NOT true」,因此要選出不符合這些特性的敘述。
選項分析
第 9 題4 分
Which of the following are true about a function which is templated?
(A) It is itself not a function, but a way to create a function.
(B) It contains the keyword template.
(C) It makes use of a template parameter to represent a calling type.
(D) none of the above
登入後即可作答並保存紀錄。
核心觀念
這題考 C++ 的函式樣板(function template)。函式樣板描述一種可依型別參數產生函式的形式;編譯器在呼叫時,會依據引數型別推導樣板參數,再產生對應的函式實例。
例如:
template <typename T>
T maximum(T a, T b) {
return a > b ? a : b;
}
maximum 是函式樣板。呼叫 maximum(3, 5) 時,編譯器可推導 T 為 int,並產生可處理 int 的函式實例。
解題方法
逐項判斷各選項是否符合函式樣板的定義:留意「函式樣板」與「由樣板實例化後的函式」是不同概念,也要確認樣板參數的用途是代表型別或值。
選項分析
第 10 題4 分
Which of the following describes the term “memory leak”?
(A) An undeclared array.
(B) A pointer that points to the value nullptr.
(C) Memory that was allocated by new but not deleted with delete.
(D) Memory that is untyped.
(E) none of the above
登入後即可作答並保存紀錄。
核心觀念
記憶體洩漏(memory leak)是指程式動態配置了記憶體,卻未適時釋放,導致這段記憶體在程式執行期間持續被占用。若程式失去指向該記憶體的指標,便無法再透過原有方式釋放它。
在 C++ 中,使用 new 配置的記憶體通常要用 delete 釋放;使用 new[] 配置的陣列則要用 delete[] 釋放。
解題方法
逐一比對選項與「已配置的記憶體未釋放」這項定義。題目採複選,但只有符合記憶體洩漏定義的敘述才應選取。
選項分析
- (A) An undeclared array. 錯誤。未宣告的陣列不是已配置但未釋放的記憶體;在 C++ 中,直接使用未宣告的陣列會造成編譯錯誤。
第 11 題5 分
You are using a dynamic array (table) that doubles when full and halves when it is full.
Which sequence results in the highest amortized cost per operation?
(A) insertions into an empty table
(B) insertions followed by deletions
(C) Alternate insert/delete: insert, delete, insert, delete... for times ( insertions, deletions)
(D) Insert times, delete all, insert times again
(E) none of the above
登入後即可作答並保存紀錄。
核心觀念
動態陣列在滿時加倍、在使用量 時減半。每次加倍或減半都要把現有元素全部搬到新陣列。均攤分析(amortized analysis)看的是「一串操作的總成本 ÷ 操作數」。因為擴張與縮小的門檻之間留有緩衝(1 與 1/4),不會發生在同一個邊界來回反覆擴縮的情況,所以每種操作序列的均攤成本都是 ——題目要比較的是哪一種序列的常數最大。
解題方法(每次插入/刪除計 1,每搬移一個元素計 1, 很大時估計)
- (A) 連續 次插入:插入本身 ;加倍時搬移 。總成本約 ,均攤約 3。
- (B) 次插入後 次刪除:插入 ,搬移約 ;刪除後仍剩 個(使用量約 1/2),不會觸發縮小,刪除只花 。總成本約 ÷ 次操作,均攤約 2.3。
- (C) 插入、刪除交替 回:陣列只在 0~1 個元素之間變動,最多在開頭擴張一次,之後既不會滿也不會再縮,每次操作成本約 1,均攤約 1。
第 12 題5 分
A stack supports operations push, pop, and multipop(k) (which pops up to elements).
Let be the stack and be the stack size. Consider the potential function:
Assume:
pushcosts .popcosts .multipop(k)costs the number of elements actually popped.
The amortized cost of multipop(k) can be described as a function . Find the true statements below.
(A)
(B)
(C)
(D) The amortized cost of multipop(k) also depends on the current stack size .
(E) none of the above
登入後即可作答並保存紀錄。
核心觀念
勢能法定義操作的攤銷成本為
其中 是實際成本, 是勢能變化。本題的勢能函數是 ,因此堆疊每少一個元素,勢能就減少 。
解題方法
設操作前堆疊有 個元素。multipop(k) 實際彈出的元素數為
實際成本是 。操作後堆疊少了 個元素,所以勢能變化為
因此攤銷成本為
無論 是多少,multipop(k) 的攤銷成本都為 ,與目前堆疊大小無關。
選項分析
第 13 題5 分
Consider the flow network shown below.
🖼️【此處有附圖,請對照原卷】
After three augmenting paths have already been applied, the residual graph contains a backward edge with capacity . Which of the following statements are true?
(A) The current flow is maximum.
(B) The flow violates capacity constraints.
(C) The residual graph must contain a cycle.
(D) A previous augmenting path used that edge forward.
(E) none of the above
圖看不清楚?展開原卷第 7 頁核對
登入後即可作答並保存紀錄。
核心觀念
本題考查殘餘網路、反向邊,以及最大流的判定。
對原網路中的邊 ,若容量為 、目前流量為 ,則:
- 正向殘餘容量為 ,表示還能增加的流量。
- 反向殘餘容量為 ,表示可以撤回的既有流量。
**增廣路徑定理:**一個可行流是最大流,若且唯若殘餘網路中不存在從 到 的增廣路徑。反向邊本身不代表違反容量限制。
解題方法
圖中斜邊的方向為 ,殘餘容量為 ;容量為 的反向邊是 ,對應原網路中流量/容量為 的 。此外,殘餘圖具有 、、,容量分別為 、、。
先從 出發,沿正殘餘容量的邊找出所有可達節點:
- 從 可以到達 、。
- 從 可以到達 。
- 與 都已滿載,因此沒有這兩條正向殘餘邊; 也已滿載。
所以可達集合為
與 均不可達,故不存在 到 的增廣路徑,目前流量已經最大。
也可用割容量驗算。目前流量為
第 14 題5 分
Consider a simple graph with and
What are the biconnected components?
(A)
(B)
(C)
(D)
(E) none of the above
登入後即可作答並保存紀錄。
核心觀念
雙連通分量(biconnected component,又稱 block)是極大的雙連通子圖:子圖連通,且刪除其中任一頂點後仍保持連通。兩個雙連通分量可以共享一個割點,但不能因為共享割點就合併成同一個分量。
判斷時可找出圖中的割點,再依照各部分的連接方式確認分量。若兩個環共享一條邊,兩個環屬於同一個雙連通分量;若兩部分只共享一個割點,則分屬不同分量。
解題方法
觀察邊可找出四個環:
前兩個環共享邊 ,因此合併成同一個極大雙連通分量 。第三個環只在 與前述部分相接,第四個環只在 與前述部分相接,因此完整的雙連通分量為:
其中 和 是割點:刪除 會使 與其餘頂點分離;刪除 會使 與其餘頂點分離。
選項分析
第 15 題2 分
Assume a hash table with slots that uses linear probing and the hash function . Insert the keys into the table in this order. Show the resulting array representation of the hash table (use “_” for empty slots).
(A)
(B)
(C)
(D)
(E) none of the above
登入後即可作答並保存紀錄。
核心觀念
線性探測(linear probing)是開放定址法的一種。插入鍵值 時,先檢查雜湊位置 ;若該槽已被占用,就依序檢查下一格,直到找到空槽。表格有 個槽時,第 次探測的位置為
本題有 個槽,槽位編號為 至 ,雜湊函數為 。
解題方法
依題目給定順序逐一插入,遇到碰撞就向右探測;探測到槽位 後,下一格會循環回槽位 。
| 插入鍵值 | 初始雜湊位置 | 線性探測結果 |
|---|---|---|
| 槽位 為空,放入 | ||
| 槽位 已占用,下一格槽位 為空,放入 | ||
| 槽位 已占用,槽位 為空,放入 | ||
| 槽位 已占用,槽位 為空,放入 | ||
| 槽位 已占用,槽位 為空,放入 | ||
| 槽位 已占用,槽位 為空,放入 |
第 16 題2 分
Suppose we are debugging a Quicksort implementation that is intended to sort an array in ascending order. After the first partition step has been completed, the contents of the array are:
Which of the following statements are incorrect about the Quicksort?
(A) Quicksort is a stable sorting algorithm because elements equal to the pivot always preserve their relative order during partitioning.
(B) If the pivot is always chosen as the smallest or largest element, Quicksort still produces a correctly sorted array, even though its running time becomes .
(C) The pivot could have been , but could not have been .
(D) Quicksort always runs in time because each partition step divides the array into two nearly equal parts.
登入後即可作答並保存紀錄。
核心觀念
本題考查快速排序(Quicksort)的分割性質、穩定性,以及不同樞紐選擇對正確性與時間複雜度的影響。
分割完成後,樞紐左側的元素應不大於樞紐,右側的元素應不小於樞紐;樞紐本身會落在分割位置。快速排序不是穩定排序,且只有分割大致平衡時,時間複雜度才是 。
解題方法
觀察分割後的陣列:
逐一檢查可能的樞紐:
- 若樞紐是 ,左側的 都小於 ,右側的 都大於 ,符合分割性質。
- 若樞紐是 ,它右側的 小於 ,不符合分割性質。
因此,選項 (C) 正確。題目問「哪些敘述錯誤」,再逐項判斷其餘選項。
選項分析
(A) 錯誤。 快速排序一般不是穩定排序。分割時,元素可能因交換而改變相同鍵值元素的相對次序;「等於樞紐的元素一定保持原順序」並非快速排序的保證。
For determining the articulation points in the following graph, begin the depth-first search at vertex . If multiple unvisited vertices are available during the search, select the vertex with the smallest alphabetical label.
🖼️【此處有附圖,請對照原卷】
第 17 題2 分
List the depth-first search numbers (starting from ) for each vertex in the order .
圖看不清楚?展開原卷第 8 頁核對
登入後即可作答並保存紀錄。
核心觀念
深度優先搜尋(DFS)從起點開始,依序選擇尚未拜訪的相鄰頂點;若有多個選擇,就依題目規定選字母較小者。每個頂點第一次被拜訪時,依序編上搜尋編號 。
解題方法
圖中的邊形成三個三角形 、、;此外, 與 相連, 與 相連。從 開始,依字母順序逐層搜尋:
- 的未拜訪鄰點為 ,先走 :。
- 從 走到尚未拜訪的 :。
- 從 走到 :。
- 從 的未拜訪鄰點 中先走 :。
- 從 的未拜訪鄰點 中先走 :。
- 從 先走 :;再從 走到 :。
- 回溯至 後,拜訪尚未拜訪的 :。
第 18 題2 分
What are the output values of for each vertex in the given graph, listed in the order of , when determining the articulation points from the previous question?
(A)
(B)
(C)
(D)
(E) none of the above
圖看不清楚?展開原卷第 8、9 頁核對
登入後即可作答並保存紀錄。
核心觀念
本題考無向圖深度優先搜尋(DFS)的探索編號 與最低可達編號 。
表示:從 出發,沿 DFS 樹邊往子孫走,再至多經過一條回邊,所能到達的最小探索編號。計算公式為:
無向圖中,連向父節點的邊是原本的樹邊,不能再當成回邊;子節點的 值則在搜尋返回時傳給父節點。
解題方法
由原圖可讀出三個三角形:、、;三者依序以 、 連接。從 開始搜尋,每次選擇字母最小的未拜訪鄰點。
1. 建立 DFS 順序與樹邊
探索順序為:
接著逐層返回 ,再拜訪 。因此:
| 頂點 | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 4 | 3 | 6 | 5 | 9 | 7 | 8 |
DFS 樹邊為 、、、、、、、。其餘三條邊形成回邊:
2. 由子節點往父節點計算
先處理 這一支。 有回邊連到 ,故:
接著處理 。 有回邊連到 ,所以:
第 19 題2 分
Based on the results of the previous two questions and the given graph, identify the articulation points.
(A)
(B)
(C)
(D)
(E) none of the above
圖看不清楚?展開原卷第 9 頁核對
登入後即可作答並保存紀錄。
讀圖
題組附圖的無向圖:、、、、、、、、、、。
核心觀念
用 DFS 判斷割點:
- 根節點:DFS 樹中有兩個以上子節點才是割點。
- 非根節點 :存在子節點 使 時是割點。
解題方法
依前兩題,從 開始、字母小者優先的 DFS:
| 頂點 | A | B | C | D | E | F | G | H | I |
|---|---|---|---|---|---|---|---|---|---|
| dfn | 1 | 2 | 4 | 3 | 6 | 5 | 9 | 7 | 8 |
| low | 1 | 1 | 4 | 1 | 6 | 4 | 4 | 6 | 6 |
DFS 樹:,以及 。
- A(根):只有一個子節點 B → 不是割點。
- B:子節點 D 的 → 不是。
For Questions 1 to 24, select multiple correct answers. Choose all (one or more) that apply. No partial credit is given.
第 20 題3 分
Given the fully parenthesized arithmetic expression below, construct the corresponding expression tree and determine its pre-order traversal.
(A)
(B)
(C)
(D)
(E) none of the above
登入後即可作答並保存紀錄。
核心觀念
二元運算式的運算式樹中,運算子是內部節點,運算元是葉節點。前序走訪依照「根、左子樹、右子樹」的順序輸出,因此每個子運算式都必須依原式的括號結構排列。
解題方法
先依括號找出最外層運算。原式最外層是減法,左側為加法子式,右側為 :
依此逐層拆解,運算式樹如下:
-
/ \
+ /
/ \ / \
- G H I
/ \
- /
/ \ / \
× × E F
/ \ / \
A B C D
由根節點開始,依序走訪左子樹、右子樹,得到前序序列:
第 21 題3 分
The following directed graph represents a project network, where each node denotes an event and each directed edge represents an activity with its duration. What is the critical path of the project?
🖼️【此處有附圖,請對照原卷】
(A)
(B)
(C)
(D)
(E) none of the above
圖看不清楚?展開原卷第 9 頁核對
登入後即可作答並保存紀錄。
核心觀念
專案網路的關鍵路徑是起點到終點之間,總工期最長的路徑。各活動所需時間相加後,最長路徑的長度就是專案最短完工時間;若路徑上有活動延誤,整個專案也會延誤。
解題方法
圖中由起點 1 到終點 10 的活動時間如下: 為 5、 為 6; 為 3、 為 6、 為 3; 為 4、 為 3、 為 5; 為 1、 為 4; 為 5、 為 2; 為 4、 為 2。
依節點順序計算各事件的最早發生時間:到達同一節點的不同路徑,要取其中較晚的時間。
第 22 題3 分
Given the following failure function in the KMP string-matching algorithm and a string “AAABAAAABAAAB” with characters, what are the values of the failure function for the entire string?
(A)
(B)
(C)
(D)
(E) none of the above
登入後即可作答並保存紀錄。
核心觀念
KMP 的失敗函數 : 中「既是前綴、又是後綴」的最長真子字串,其最後一個字元的索引 ;不存在則為 。遞迴計算:令 ,若 則 ;否則令 再比,直到 時比較 。
解題方法
字串 AAABAAAABAAAB(13 個字元):
| j | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| A | A | A | B | A | A | A | A | B | A | A | A | B | |
| −1 | 0 | 1 | −1 | 0 | 1 | 2 | 2 | 3 | 4 | 5 | 6 | 3 |
關鍵幾格:
- :B 與開頭的 A 不符 → 。
- :,比 ;
第 23 題3 分
Given the following in-order and post-order traversals of a binary tree, reconstruct the binary tree uniquely determined by these traversals, and then delete node from the tree. What is the level-order traversal of the resulting tree after the deletion?
In-order:
Post-order:
(A)
(B)
(C)
(D)
(E) none of the above
登入後即可作答並保存紀錄。
核心觀念
中序走訪依序為「左子樹、根、右子樹」;後序走訪依序為「左子樹、右子樹、根」。因此,每個子樹在後序序列中的最後一個節點是該子樹的根;在中序序列找到根後,即可分出左右子樹,並遞迴重建。
本題是一般二元樹,節點標記不代表大小關係。刪除一般二元樹節點時,採常見作法:以層序中最深、最右的節點取代欲刪除節點,再移除該最深最右節點。
解題方法
後序序列最後一個節點為 ,所以根節點是 。在中序序列中, 左側沒有節點,右側包含其餘所有節點,故 沒有左子樹。
依此方式逐層分割,可得:
- 的右子節點為 ; 的右子節點為 。
- 的左子節點為 。
- 的左子樹根為 ,且 的左子節點為 。
- 的右子樹根為 ; 的左子節點為 、右子節點為 ; 的右子節點為 。
重建出的樹如下:
第 24 題3 分
Determine the order in which vertices are selected by Dijkstra’s algorithm on the graph below, starting from source vertex . If two unvisited vertices have the same tentative distance, select the vertex with the smaller alphabetical label first.
🖼️【此處有附圖,請對照原卷】
(A)
(B)
(C)
(D)
(E) none of the above
圖看不清楚?展開原卷第 10 頁核對
登入後即可作答並保存紀錄。
核心觀念
Dijkstra 演算法每次從尚未選取的頂點中,選出目前暫定距離最小者,再用它更新相鄰頂點的距離。若暫定距離相同,依題意選字母較小的頂點。
解題方法
圖中從 出發的邊與權重為:、、、、、、、、。依圖上的連線逐步更新暫定距離,選取順序如下:
| 步驟 | 選取頂點 | 選取後更新的暫定距離 |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ;經 到 為 ,不優於 |
第 25 題9 分
[Operator Overloading] Vector is widely used in the engineering and science world. Suppose we create a class called vector_2d. It can be used to represent a 2D vector, with and as its values. The partial code of this vector is shown below.
class vector_2d {
private:
double x;
double y;
public:
vector_2d() { x = 0; y = 0; }
vector_2d(double x_, double y_) { x = x_; y = y_; }
double get_x() const { return x; }
double get_y() const { return y; }
// Add overloaded operator+= here
}
Implement operator+= for vector_2d as a member function. If and , after a += b, becomes , whereas is unchanged. Write no more than lines of code.
登入後即可作答並保存紀錄。
核心觀念
operator+= 是複合指定運算子的多載。以成員函式實作時,運算左側的物件就是呼叫函式的物件,可透過 this 或直接存取成員變數修改它。
對二維向量而言,向量加法是逐分量相加:
慣例上,operator+= 回傳左側物件的參考,讓運算結果仍代表更新後的物件。
解題方法
在函式中將參數的 x、y 分別加到目前物件的 x、y,最後回傳 *this。參數使用常數參考,避免複製物件,也確保函式不修改右側物件。