免費詳解 / 中正 / 軟體 / 105

105 中正資工所考古題解答|軟體逐題詳解

105 中正資工所軟體考古題的逐題詳解,共 15 題,免費公開。

逐題詳解

15%

Write down the output of the following C program

int main(void)
{
   int a=7, b=6, c=5;
   if(a < b < c)
      printf("Yes\n");
   else
      printf("No\n");
   return 0;
}

關係運算子左結合:a < b < c = (a < b) < c

7 < 6 得 0,0 < 5 得 1,條件成立

要判斷「b 介於 a 與 c 之間」得寫 a < b && b < c

Yes

25%

Write down the output of the following C program

int main()
{
    int i, count=0;
    for(i=0; i<35; i++){
        if(i%5==0) continue;
        count++;
    }
    printf("%d\n", count);
    return 0;
}

\(i=0,1,\dots,34\) 共 35 次;\(i\%5==0\) 的是 \(0,5,10,\dots,30\) 共 7 個,被 continue 跳過,不做 count++

\(35-7=28\)

28

35%

Write down the output of the following C program

int main()
{
    for(int i; i<=9; i++)
    {
        if(i%2)
            if(i%3)
                printf("A");
        else
            printf("B");
    }
    return 0;
}

else 一律和最接近、還沒配對的 if 綁在一起,所以縮排雖然對齊外層,實際結構是

if (i % 2) {
    if (i % 3) printf("A");
    else       printf("B");
}

偶數 \(i\) 什麼都不印;奇數 \(i\) 看能不能被 3 整除:

\(i\)13579
\(i\%3\)10210
輸出ABAAB

for(int i; …) 沒給 i 初值,嚴格說是 undefined behavior;這裡照題意當作從 0 開始

ABAAB

45%

Write down the output of the following C program

void main()
{
   int s[5]={1, 3, 9, 6, 4};
   int *p=s , *ptr=s+2 ;
   printf("A:%d\n", *p+1);
   printf("B:%d\n", *ptr);
   printf("C:%d\n", s[0]);
   printf("D:%d\n", *p++);
   printf("E:%d\n", *p);
}

\(s=\{1,3,9,6,4\}\),p 指向 s[0],ptr 指向 s[2]

A:*p+1 是 (*p)+1(* 優先權高於 +)= \(1+1=2\);p 不動

B:*ptr = s[2] = 9

C:s[0] = 1

D:*p++:先取目前的 *p(1)給 printf,再把 p 往後移一格 → 1

E:p 已指向 s[1] → 3

*p+1 是「值加 1」;要取下一個元素得寫 *(p+1)(那會是 3)

A:2 B:9 C:1 D:1 E:3(每個一行)

55%

Write down the output of the following C program

int func (int a, int b)
{
    if (a < b)
        return func(b, a);
    if (b == 0)
        return a;
    else
        return func(b, a%b);
}
int main(void){
    int a, b;
    printf("%d", func(30, 75));
    return 0;
}

輾轉相除法求 gcd:

呼叫動作
func(30,75)\(a\lt b\),改呼叫 func(75,30)
func(75,30)\(b\ne0\),func(30, 75%30=15)
func(30,15)func(15, 30%15=0)
func(15,0)\(b=0\),回傳 15

15

64%

Choose the correct answer (as far as the syntax of C++ language is concerned).

甲. Member functions always have to be public. (a) True (b) False

乙. If s1 and s2 are structs, s1 == s2 checks if all their data members are identical. (a) True (b) False

丙. It is possible for a class to have several different constructors. (a) True (b) False

丁. It is possible for a class to have several different destructors. (a) True (b) False

甲 member function 可以是 private 或 protected,只給類別內部或衍生類別用 → (b)

乙 struct 沒有預設的 operator==(C++20 前),s1 == s2 不會編譯,更不會逐一比對 data member → (b)

丙 constructor 可以多載,只要參數列不同 → (a)

丁 destructor 不接參數、不能多載,一個類別只有一個 → (b)

甲 (b) 乙 (b) 丙 (a) 丁 (b)

75%

Choose the correct answer (as far as the syntax of C++ language is concerned). Consider the following code:

int a[2];
int* x = new int[3];
int* y = new int[5];
int* t = y;
y = x;
x = a;
delete t;
delete y;
delete x;

This code compiles. However, what bug(s) does it have? (each: True / False)

甲. cannot delete t, since it was not initialized using new 乙. has memory leak 丙. mixes arrays of different lengths 丁. tries to deallocate a statically-allocated array 戊. wrong deallocation syntax

先追蹤三個指標在三個 delete 時各指向哪裡:

指標指向
tnew int[5] 那塊(由 y 抄來)
ynew int[3] 那塊(y = x)
x靜態陣列 a(x = a)

甲 t 指向 new 配出來的記憶體 → F

乙 兩塊 heap 記憶體都被釋放(一塊由 t、一塊由 y),沒有洩漏 → F

丙 不同長度的陣列互相指派指標本身合法,問題不在長度 → F

丁 delete x 時 x 指向 stack 上的 a,對它 delete 是嚴重錯誤 → T

戊 兩塊 heap 都是 new[] 配的,要用 delete[],寫成 delete 是 undefined behavior → T

甲 (b) 乙 (b) 丙 (b) 丁 (a) 戊 (a)

88%

Choose the correct answer (as far as the syntax of C++ language is concerned).

The getline function 甲. skips all whitespace. 乙. can write data directly to an int. 丙. can cause stream to fail if there is no more data to read. 丁. can be used with an ofstream. (each: (a) True (b) False)

The >> operator 戊. skips all whitespace. 己. can write data directly to an int. 庚. can cause stream to fail if there is no more data to read. 辛. can be used with an ofstream. (each: (a) True (b) False)

getline

甲 整行讀進來,空白照單全收,不會跳過 → F

乙 只讀進 string 或 char 陣列,不能直接填進 int → F

丙 讀到檔尾沒有資料可讀時會設 failbit → T

丁 getline 是輸入用的,ofstream 是輸出串流 → F

>> operator

戊 先跳過 leading whitespace 再讀 → T

己 cin >> n 會轉型後直接寫進 int → T

庚 沒有資料可讀時同樣讓串流進入 fail 狀態 → T

辛 >> 是 extraction,對應 istream;ofstream 要用 << → F

甲 (b) 乙 (b) 丙 (a) 丁 (b) 戊 (a) 己 (a) 庚 (a) 辛 (b)

98%

Choose the best answer (as far as the syntax of C++ language is concerned).

甲. When an object X of a derived class is assigned, using the default assignment operator, to a variable Y of its corresponding base type: (a) Y has all the data and function members of the derived class. (b) Y has only the data and function members of the base class. (c) an error occurs because this is not an allowed operation. (d) none of the above

乙. If a class C is derived from class B, which is derived from class A, all through public inheritance, then a class C member function can access (a) protected and public data only in C and B. (b) protected and public data only in C. (c) private data in A and B. (d) protected data in A and B.

丙. Usually a pure virtual function (a) has complete function body. (b) will never be called. (c) will be called only to delete an object. (d) is defined only in derived class.

丁. In which case is it mandatory to provide a destructor in a class? (a) Almost in every class. (b) Class for which two or more than two objects will be created. (c) Class for which copy constructor is defined. (d) Class whose objects will be created dynamically.

甲 derived 物件指派給 base 型別的變數,發生 object slicing:只複製 base 的部分,derived 多出來的成員被切掉 → (b)

乙 public 繼承下,C 的 member function 看得到 A 與 B 的 public 與 protected 成員,private 成員不論隔幾層都碰不到;四個選項只有 (d) 的敘述成立((a)(b) 漏掉了 A 或 B,(c) 不可能) → (d)

丙 pure virtual function(= 0)在 base class 通常只有宣告、沒有實作,實作寫在 derived class 的 override 裡 → (d)

丁 物件是動態配置(內部持有 new 出來的資源)時,釋放資源的責任落在 destructor,這時非寫不可 → (d)

丙的 (b)「will never be called」若指 base 版本確實不會被呼叫,但實際執行的是 derived 的 override;選項裡「定義只出現在 derived class」才是 pure virtual 的常態

甲 (b) 乙 (d) 丙 (d) 丁 (d)

1010%

(a) What is the average case time complexity to search in a binary tree of N nodes? (b) What is the average case time complexity to search in a sorted linked list of N nodes? (c) What is the worst case time complexity to search in an AVL tree of N nodes? (d) What is the worst case time complexity of Breadth-First-Search algorithm for a graph with V nodes and E edges? (e) What is the worst case time complexity of Dijkstra's Shortest Path Algorithm for a graph with V nodes and E edges?

(a) \(O(\log N)\):以 binary search tree 計,平均每次比較砍掉約一半,平均樹高 \(O(\log N)\)

(b) \(O(N)\):linked list 排序過也不能隨機跳到中間,只能從頭一個一個走

(c) \(O(\log N)\):AVL tree 靠旋轉維持平衡,最壞樹高 \(\le1.44\log_2N\)

(d) \(O(V+E)\):每個頂點進 queue 一次、每條邊檢查一次(adjacency list)

(e) \(O(E\log V)\):adjacency list + binary heap;Fibonacci heap 為 \(O(E+V\log V)\),用陣列找最小則是 \(O(V^2)\)

(a) 若指的是沒有排序規則的一般 binary tree,要走訪整棵樹,平均是 \(O(N)\);這裡與 (b)(c) 並列比較搜尋樹,取 \(O(\log N)\)

(a) \(O(\log N)\) (b) \(O(N)\) (c) \(O(\log N)\) (d) \(O(V+E)\) (e) \(O(E\log V)\)

1115%

a). (8%) Please write a C function to search in a Binary Search Tree. Assume that each node contains a key that is a string. Return the node containing the matched key. You have to write the data structure of the tree node.

b). (7%) Please write a C function to insert a new node to the tail of a linked list. Suppose that each node contains a key that is of integer type. You have to write the data structure of the linked list node.

(a)

#include <string.h>

typedef struct TreeNode {
    char *key;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

TreeNode *bst_search(TreeNode *root, const char *key) {
    while (root != NULL) {
        int cmp = strcmp(key, root->key);
        if (cmp == 0)
            return root;                       /* 找到,回傳該節點 */
        root = (cmp < 0) ? root->left : root->right;
    }
    return NULL;                               /* 樹裡沒有這個 key */
}

字串的大小比較用 strcmp,不能用 == 或 <(比的是位址不是內容)

(b)

#include <stdlib.h>

typedef struct ListNode {
    int key;
    struct ListNode *next;
} ListNode;

void insert_tail(ListNode **head, int key) {
    ListNode *node = malloc(sizeof(ListNode));
    if (node == NULL) return;
    node->key = key;
    node->next = NULL;
    if (*head == NULL) {                       /* 空串列,新節點就是 head */
        *head = node;
        return;
    }
    ListNode *cur = *head;
    while (cur->next != NULL)                  /* 走到最後一個節點 */
        cur = cur->next;
    cur->next = node;
}

head 用 ListNode ** 傳入,空串列時才改得到呼叫端的指標;另外存一個 tail 指標,這個操作可從 \(O(n)\) 降到 \(O(1)\)

129%

True or False(每答錯一小題倒扣該小題分數直至本題零分為止。)

(a) (3%) Quicksort is asymptotically optimal (b) (3%) When a given set of numbers makes the partitioning of Quicksort maximally unbalanced at every recursive level, the corresponding decision tree is a full binary tree. (c) (2%) Insertion sort is not stable (d) (1%) Hamiltonian cycle is an NP-complete problem

(a) asymptotically optimal 要求 worst case 達到比較排序的下界 \(\Omega(n\log n)\);Quicksort 的 worst case 是 \(\Theta(n^2)\)(達到的是 merge sort、heap sort) → F

(b) 這題的用語(asymptotically optimal、decision tree)出自 CLRS。CLRS 的 full binary tree 是「每個 node 不是 leaf 就是恰有兩個 child」;比較排序的 decision tree 每個內部節點是一次比較、分成 ≤ 與 > 兩支,所以不論輸入怎麼排都是 full binary tree。就算看的是最不平衡時的遞迴樹,每次 partition 也分成 \(n-1\) 與 \(0\) 兩個子問題,每個內部節點仍有兩個 child → T

(c) insertion sort 只有碰到嚴格較大的才往後搬,相等的不交換,是 stable → 「not stable」為 F

(d) Hamiltonian cycle 的判定問題是 Karp 21 個 NP-complete 問題之一 → T

(b) 若把 full binary tree 當成「每一層都填滿」(深度 \(k\) 有 \(2^k-1\) 個 node 的定義),最不平衡時遞迴樹是一條往一邊長的鏈,就是 F;題目用的是 CLRS 的用語,照 CLRS 的定義答 T

(a) F (b) T (c) F (d) T

135%

(5% total; 1% each) Determine which of Floyd-Warshall algorithm, Depth first search, greedy algorithm, or dynamic programming could be the best fit to solve the following problems: (a) Minimum Spanning Tree; (b) 0-1 Knapsack problem; (c) construct Huffman coding; (d) topological sort; (e) construct an optimal binary search tree.

(a) MST:Kruskal、Prim 每步挑當下最小的合法邊 → greedy algorithm

(b) 0-1 Knapsack:物品不可分割,靠「前 \(i\) 個物品、容量 \(w\)」的表格逐格推 → dynamic programming

(c) Huffman coding:每次合併頻率最小的兩棵樹 → greedy algorithm

(d) topological sort:對 DAG 做 DFS,依 finish time 由大到小輸出 → depth first search

(e) optimal BST:\(e\)、\(w\)、\(root\) 三張表由小區間往大區間填 → dynamic programming

(a) greedy (b) DP (c) greedy (d) DFS (e) DP

146%

Use substitution method to prove a tight asymptotic lower bound for the recurrence: \(T(n)=4T(n/2)+n^2\lg n\).

先猜:\(a=4,\ b=2,\ n^{\log_ba}=n^2\),\(f(n)=n^2\lg n=\Theta(n^2\lg^1n)\),是 master theorem case 2 的推廣型(\(k=1\)) → 猜 \(T(n)=\Theta(n^2\lg^2n)\)

證 \(T(n)\ge c\,n^2\lg^2n\)(取 \(c=\tfrac12\),\(n\) 取 2 的冪)。基底 \(n=1\):\(c\cdot1\cdot0=0\le T(1)\)

歸納假設對所有 \(k\lt n\) 成立:

\(T(n)=4T(n/2)+n^2\lg n\ge4\cdot\tfrac12\left(\tfrac n2\right)^2\lg^2\tfrac n2+n^2\lg n\)

\(=\tfrac12n^2(\lg n-1)^2+n^2\lg n\)

\(=\tfrac12n^2\lg^2n-n^2\lg n+\tfrac12n^2+n^2\lg n\)

\(=\tfrac12n^2\lg^2n+\tfrac12n^2\ \ge\ \tfrac12n^2\lg^2n\) 歸納成立

上界同樣可證 \(T(n)\le c'n^2\lg^2n\),所以是緊的:\(\Theta(n^2\lg^2n)\)

\(T(n)=\Omega(n^2\lg^2n)\)

155%

Given a chain of \(n\) matrices \(A_1,A_2,\dots,A_n\) where the dimension of \(A_i\) is \(d_{i-1}\times d_i\), if we want to exhaustively check all possible parenthesizations of the \(n\)-matrices multiplication, (a) derive a recurrence for the number of alternative parenthesizations, \(P(n)\) (2%); and (b) prove that it is exponential to \(n\) (3%).

(a) \(n\ge2\) 時最外層那次乘法把連乘切成 \(A_1\cdots A_k\) 與 \(A_{k+1}\cdots A_n\),切點 \(k=1,\dots,n-1\),左右兩段各自的括號法互相獨立:

\(P(n)=\begin{cases}1&n=1\\[2pt]\displaystyle\sum_{k=1}^{n-1}P(k)P(n-k)&n\ge2\end{cases}\)

前幾項 \(1,1,2,5,14,\dots\)(Catalan 數 \(C_{n-1}\))

(b) 證 \(P(n)\ge2^{n-2}\):

基底:\(P(1)=1\ge2^{-1}\)、\(P(2)=1\ge2^0\)、\(P(3)=2\ge2^1\)、\(P(4)=5\ge2^2\)

\(n\ge5\),假設 \(P(k)\ge2^{k-2}\) 對 \(k\lt n\) 成立:

\(P(n)=\sum_{k=1}^{n-1}P(k)P(n-k)\ge\sum_{k=1}^{n-1}2^{k-2}\cdot2^{n-k-2}=(n-1)\,2^{n-4}\ge4\cdot2^{n-4}=2^{n-2}\)

所以 \(P(n)=\Omega(2^n)\) 得證

窮舉所有括號法至少要 \(2^{n-2}\) 種,必然是指數時間,這也是矩陣連乘要改用 DP 的理由

109–115 的詳解也是這樣寫的。

國立中正大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 351 頁,逐題推導。

購買 · NT$ 850 看中正其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

題目有錯、詳解看不懂,都歡迎來信

想許願補哪一份、對購買有疑問也可以,收到後會盡快回覆。

william.chou.123123@gmail.com